I have 2 tables (example):
users:
ID company_ID
1 7
2 6
3 7
activity_rewards:
user_ID points activity_type_ID
1 1 7
1 2 7
1 1 7
1 1 8
2 1 7
2 1 7
2 2 8
2 1 7
3 2 7
3 1 7
3 2 8
3 1 8
(There are also tables for company and activity_types, but they shouldn't be relevant here)
I need a MYSQL query that will sum the total points for each user WHERE all users have a certain company_ID and for a certain activity_type_ID AND it will return the MAX and the AVG of the sum of all the users points
I have for example:
SELECT SUM(activity_rewards.points) AS totalpoints,
MAX(activity_rewards.points) AS maxpoints,
AVG(activity_rewards.points) AS avgpoints
FROM activity_rewards
INNER JOIN users
ON activity_rewards.user_ID = users.ID
WHERE ( (users.company_ID = "7") && (activity_rewards.activity_type_ID LIKE '8') )
In the example query only 3 results are involved. They are:
user_ID points activity_type_ID
1 1 8
3 2 8
3 1 8
I want to get:
user 1 has 1 point
user 3 has 3 points
Max is 3 average is 2
instead I'm getting Max is 2 and average is 1.33
The results are in line with your query, but your query is not in line with your requirements.
Your query calculates the max and the average of points across the relevant records. But you seem to want the max and the average of the points summed by user id.
This means that you need to calculate the sum of points per user in a subquery and then calculate the max and average in the outer query.
SELECT SUM(sumpoints) as totalpoints, max(sumpoints) as maxpoints, avg(sumpoints) as avgpoints
FROM
(SELECT users.ID, SUM(activity_rewards.points) AS sumpoints
FROM activity_rewards
INNER JOIN users ON activity_rewards.user_ID = users.ID
WHERE users.company_ID = 7 and activity_rewards.activity_type_ID = 8
GROUP BY users.ID) t
Do it like this:
SELECT MAX(sum_points) max, AVG(sum_points) avg, SUM(sum_points) sum_all FROM (SELECT SUM(t2.points) sum_points FROM users t1 JOIN activity_rewards t2 ON (t1.ID = t2.user_ID) WHERE ( (t1.company_ID = "7") AND (t2.activity_type_ID = '8') ) GROUP by t1.ID) as summation
Related
I m new to this community and also new to MySQL.
Need to COUNT the clients and SUM the commission for each referrer respectively using MySQL query.
referrer table
id
referrer_name
1
referre1
2
referre2
3
referre3
4
referre4
client table
id
referrer_id
client_name
1
1
client1
2
1
client2
3
2
client3
4
3
client4
5
4
client5
commission table
id
client_id
commission
1
1
20
2
1
32
3
3
24
4
4
15
I want to achieve below output using above 3 tables.
referrer_id
referrer_name
total_client
total_commission
1
referre1
2
52
2
referre2
1
24
3
referre3
1
15
4
referre4
1
0
I have tried the following query
SELECT referrer.referrer_name as name,
(SELECT COUNT(*) FROM client WHERE client.referrer_id=referrer.id) as total_client,
(SELECT SUM(commission) FROM commission WHERE commission.client_id=client.id) as total_commission FROM referrer LEFT JOIN client ON client.referrer_id=referrer.id GROUP BY name;
Read Mysql join and Aggregate Functions
SELECT t2.referrer_id,
t1.referrer_name,
COUNT(DISTINCT t2.id) AS total_client,
SUM(t3.commission) AS total_commission
FROM referrer t1
JOIN client t2 ON t1.id = t2.referrer_id
JOIN commission t3 ON t2.id = t3.client_id
GROUP BY t1.referrer_id
Currently I am honestly at loss what I am doing wrong. It is a rather simple query I think.
Tables:
operations:
id processedon clientid
1 2018-01-01 9
2 2018-03-16 9
3 2018-04-21 9
4 2018-04-20 9
5 2018-05-09 9
items:
id operation_id quantity unitprice
1 1 10 2
2 1 5 3
3 2 20 4
4 3 10 2
5 4 8 4
6 4 10 4
7 5 2 2
The expected result of the operation/query is:
month total_value
1 35
3 80
4 92
5 4
That is quantity * unitprice based. For some reason, it only returns month=4
SELECT
month(`operations`.`processedon`) AS `month`,
SUM((`items`.`quantity` * `items`.`unitprice`)) AS `total_value`
FROM `items`
INNER JOIN `operations` ON (`items`.`operation_id` = `operations`.`id`)
GROUP BY 'month'
ORDER BY 'month'
According to the info provided the join should be
INNER JOIN operations ON items.operation_id = operations.id
Eg
SELECT
month(`operations`.`processedon`) AS `month`,
SUM((`items`.`quantity` * `items`.`unitprice`)) AS `total_value`
FROM `items`
INNER JOIN `operations` ON `items`.`operation_id` = `operations`.`id`
GROUP BY month(`operations`.`processedon`)
ORDER BY `month`
There is no efficiency gain by using a column alias in the group by clause, I prefer to avoid using them except perhaps in the order by clause.
The following query will give you the required answer
SELECT
month(`operations`.`processedon`) AS `month`,
SUM((`items`.`quantity` * `items`.`unitprice`)) AS `total_value`
FROM items
INNER JOIN operations ON (items.operation_id = operations.id)
GROUP BY month(operations.processedon)
ORDER BY month(operations.processedon)
You need to specify month correctly since it is not an existing column.
You'll get the following result
month total_value
1 35
3 80
4 92
5 4
purchased_models
user_id model_id
1 1
1 4
1 9
1 3
2 3
2 2
2 7
2 4
3 9
3 6
3 1
3 5
4 8
4 7
4 9
4 1
5 8
5 9
Using the above table, write a query to output a table the columns of which are user_id, number of models that user has purchased, and the number of people that have purchased any of the same models that the user in question has purchased. The result should look like this:
Result
user_id models_purchased users_purchasing_same_models
1 4 4
2 4 2
3 4 3
4 4 4
5 2 3
Got the first two with:
SELECT
DISTINCT user_id,
COUNT(DISTINCT model_id)
FROM
purchased_models
GROUP BY user_id
, but having trouble with the third one. Any help would be greatly appreciated. Thanks!
One way to do this is to use a correlated subquery that counts the distinct number of users that has purchased any of the models that the user referenced in the outer scope has purchased:
select
t1.user_id,
count(distinct t1.model_id) models_purchased,
(
select count(distinct user_id)
from purchased_models t2
where t2.model_id in (
select model_id
from purchased_models
where user_id = t1.user_id
and t2.user_id <> t1.user_id
)
) users_purchasing_same_models
from purchased_models t1
group by t1.user_id
order by t1.user_id
I would personaly try to self-join the table and conditional sum, as follows:
SELECT
a.user_id AS Id,
SUM(CASE WHEN a.user_id = b.user_id AND a.model_id=b.model_id THEN 1 ELSE 0 END) AS ModelCount,
SUM(CASE WHEN a.user_id != b.user_id AND a.model_id=b.model_id THEN 1 ELSE 0 END) AS Others
FROM purchased_models AS a
LEFT OUTER JOIN purchased_models AS b
ON TRUE
GROUP BY a.user_id
I've got tables:
Table1= USER_ID ITEM_ID
1 12
1 13
2 12
3 12
3 1
3 2
etc..
And second table:
Products = ITEM_ID PRICE
1 1.3
2 0.1
4 22
12 33
13 45
It is just example. How can I get ID's of clients who paid more than average order value?
I tried many times, but I always get errors.
You can do a JOIN between the tables and comparing the average price with specific user price paid like
select t1.user_id
from table1 t1
join products p on t1.item_id = p.item_id
group by t1.user_id
having p.price > avg(p.price);
I have 2 different tables in my database by the name of: rank, settings.
Here is how each table looks like with a few records in them:
Table #rank:
id points userid
-- ----- ------
1 500 1
2 300 2
3 900 3
4 1500 4
5 100 5
6 700 6
7 230 7
8 350 8
9 850 9
10 150 10
Table #settings:
userid active
------ ------
1 0
2 1
3 1
4 1
5 1
6 0
7 1
8 1
9 0
10 1
I want to get the rank of a specific user by user_id from the rank table ordering by their points. Also I would Only want to include the users in the ranking results, if they have active = 1 set in the settings table.
I have a simple ranking query, but it is not really effective, because it does include everyone even if the user is not active:
SELECT * FROM
(SELECT #sort:=#sort+1 AS sort, points, userid
FROM rank,
(SELECT #sort := 0) s
ORDER BY points DESC) t
WHERE userid= 8
Any idea, how could I achieve my goals here?
Few sub queries. First gets all the users who are active in the right order. That is used as a source for another query to add the rank. Then this is used as the source for the points and rank for the userid you are actually interested in
SELECT sort, points
FROM
(
SELECT #sort:=#sort + 1 AS sort, points, userid
FROM
(
SELECT rank.points, rank.userid
FROM rank
INNER JOIN settings
ON rank.userid = settings.userid
WHERE settings.active = 1
ORDER BY points DESC
) sub0
CROSS JOIN (SELECT #sort:=0) sub2
) sub1
WHERE sub1.userid = 8
Borrowing the idea from: https://stackoverflow.com/a/4474389/92063
SELECT
#rn:=#rn+1 AS RANK
,USER_ID
,POINTS
FROM (
SELECT
R.userid AS USER_ID
,R.points AS POINTS
FROM
rank R
INNER JOIN
settings S
ON R.userid = S.userid
WHERE
S.active = 1
ORDER BY
R.points DESC
) t1, (SELECT #rn:=0) t2;