MySQL - How to group sum top 6 and the rest - mysql

I have reports table that aggregates data each product quantity each day.
SELECT r.year, r.month, c.id, c.client_name, p.product_name, cc.country_name, sum(r.quantity) units FROM
client c
join report r on c.id = r.client_id
join product p on r.product_id = p.id
join country cc on r.country_id = c.id
WHERE r.year = year(now())
group by r.year, r.month, c.id, p.product_name, cc.country_name
I'm trying to figure out how group units sum by month, client, product and country where query shows sum for top 5 countries and rest is sum from bottom countries. Something like this:
case
when sum(r.quantity) = 'Top 1' then cc.country_name
when sum(r.quantity) = Top 2' then cc.country_name
.....
when sum(r.quantity) = 'Top 2' then cc.country_name
else 'Other'
How can I do this?
Many thanks in advance

you can try this. Here i sort the result from most quantity to less and add a row number. so the first 6 are the top.
please try it, but i cant tested.
SELECT
#nr := ( #nr +1) AS nr,
IF ( #nr < 7, CONCAT('Top ',#nr), 'other' ) AS top,
r.* FROM (
SELECT r.year, r.month, c.id, c.client_name, p.product_name, cc.country_name, sum(r.quantity) units
FROM CLIENT c
JOIN report r ON c.id = r.client_id
JOIN product p ON r.product_id = p.id
JOIN country cc ON r.country_id = c.id
WHERE r.year = YEAR(now())
GROUP BY r.year, r.month, c.id, p.product_name, cc.country_name
ORDER BY sum(r.quantity) DESC
) AS r
CROSS JOIN ( SELECT #nr:=0 ) AS params;

Related

Select players with fixed trophy count and points

I need print out players who have 2 trophies, and I need sum their all matches earned points. There i need use 3 tables from my DB
1.I need got trophy_count , I can this with this step ->
select surname, count(t.player_id) as trophy_count
from dbo.Players p
left join Trophies t on t.player_id=p.id
group by p.surname
So, SQL gave me this
2.Print out all earned points ->
select name, sum(points) as points
from dbo.Players p
inner join dbo.Stats s on s.player_id=p.id
group by p.name
SQL gave me this:
I want do this in one query:
select name, sum(points) as points, COUNT(t.player_id) as trophy_count
from dbo.Players p
inner join dbo.Stats s on s.player_id=p.id
inner join dbo.Trophies t on t.player_id=p.id
group by p.name
and SQL gave me this, SQL multiplies x 2 all my info, it is wrong
And , at this fail, I dont know what i need write, for select players who have 2 trophies and sum their earned points. (Lonzo ball 21 point 2trophies,Kristaps Porzingis 17points 2trophies).
For safer result, wrap all your current query in a subquery.
SELECT p.id, p.name, p.surname,
IFNULL(trop.trophy_count, 0),
IFNULL(pts.points, 0)
FROM dbo.Players p
LEFT JOIN
(
select p.id, count(t.player_id) as trophy_count
from dbo.Players p
left join Trophies t on t.player_id=p.id
group by p.id
) trop ON p.id = trop.id
LEFT JOIN
(
select p.id,sum(points) as points
from dbo.Players p
inner join dbo.Stats s on s.player_id = p.id
group by p.id
) pts ON p.id = pts.id
Can you check this:
SELECT P.name,
(SELECT CASE WHEN (SUM(S.points) IS NULL) THEN 0 ELSE SUM(S.points) END FROM Stats S WHERE S.player_id = P.id ) as points,
(SELECT COUNT(T.player_id) FROM Trophies T WHERE T.player_id = P.id ) as trophy_count
FROM Players P
I do not have specified dbo. in front of Tables in query, if you need that please add & test.
SQL Fiddle
It will works well to your question
SELECT dbo.Players.name, dbo.Players.surname, ISNULL(s.points, 0) points, ISNULL(t.trophy_count, 0) trophies_count FROM dbo.Players
LEFT JOIN (
SELECT player_id, SUM(points) points FROM dbo.Stats
GROUP BY player_id
) s ON s.player_id = dbo.Players.id
LEFT JOIN (
SELECT player_id, COUNT(*) trophy_count FROM dbo.Trophies
GROUP BY player_id
) t ON t.player_id = dbo.Players.id
WHERE t.trophy_count = 2

Merge 2 SQL Queries/Tables

I spent so much time googling today but i don't even know which keywords to use. So …
The project is an evaluation of a betting game (Football). I have 2 SQL Queries:
SELECT players.username, players.userid, matchdays.userid, matchdays.points, SUM(points) AS gesamt
FROM players INNER JOIN matchdays ON players.userid = matchdays.userid AND matchdays.season_id=5
GROUP BY players.username
ORDER BY gesamt DESC
And my second query:
SELECT max(matchday) as lastmd, points, players.username from players INNER JOIN matchdays ON players.userid = matchdays.userid WHERE matchdays.season_id=5 AND matchday=
(select max(matchday) from matchdays)group by players.username ORDER BY points DESC
The first one adds up the points of every matchday and shows the sum.
The second shows the points of the last gameday.
My Goal is to merge those 2 queries/tables so that the output is a table like
Rank | Username | Points last gameday | Overall points |
I don't even know where to start or what to look for. Any help would be appreciated ;)
use both query with join....use inner join if each userid have value in 2nd query also.also add userid in 2nd query also for join
SET #rank = 0;
SELECT #rank := rank + 1,
t1.username,
t2.points,
t1.gesamt
FROM (
SELECT players.username, players.userid puserid, matchdays.userid muserid, matchdays.points, SUM(points) AS gesamt
FROM players INNER JOIN matchdays ON players.userid = matchdays.userid AND matchdays.season_id=5
GROUP BY players.username
)t1
INNER JOIN
(
SELECT players.userid, max(matchday) as lastmd, points, players.username
from players INNER JOIN matchdays ON players.userid = matchdays.userid
WHERE matchdays.season_id=5 AND matchday=
(select max(matchday) from matchdays)group by players.username
)t2
ON t1.puserid = t2.userid
ORDER BY t1.gesamt
You can use conditional aggregation, i.e. sum the points only when the day is the last day:
SELECT
p.username,
SUM(case when m.matchday = (select max(matchday) from matchdays) then m.points end)
AS last_day_points,
SUM(m.points) AS total_points
FROM players p
INNER JOIN matchdays m ON p.userid = m.userid AND m.season_id = 5
GROUP BY p.userid
ORDER BY total_points DESC;
Or with a join instead of a non-correlated subquery (MySQL should come to the same execution plan):
SELECT
p.username,
SUM(case when m.matchday = last_day.matchday then m.points end) AS last_day_points,
SUM(m.points) AS total_points
FROM players p
INNER JOIN matchdays m ON p.userid = m.userid AND m.season_id = 5
CROSS JOIN
(
select max(matchday) as matchday
from matchdays
) last_day
GROUP BY p.userid
ORDER BY total_points DESC;

how to sort and group by by it's count

I have the following:
SELECT DISTINCT s.username, COUNT( v.id ) AS cnt
FROM `instagram_item_viewer` v
INNER JOIN `instagram_shop_picture` p ON v.item_id = p.id
INNER JOIN `instagram_shop` s ON p.shop_id = s.id
AND s.expirydate IS NULL
AND s.isLocked =0
AND v.created >= '2014-08-01'
GROUP BY (
s.id
)
ORDER BY cnt DESC
Basically I have an instagram_item_viewer with the following structure:
id viewer_id item_id created
It tracks when a user has viewed an item and what time. So basically I wanted to find shops that has the most items viewed. I tried the query above and it executed fine, however it doesn't seem to give the appropriate data, it should have more count than what it is. What am I doing wrong?
First, with a group by statement, you don't need the DISTINCT clause. The grouping takes care of making your records distinct.
You may want to reconsider the order of your tables. Since you are interested in the shops, start there.
Select s.username, count(v.id)
From instagram_shop s
INNER JOIN instagram_shop_picture p ON p.shop_id = s.shop_id
INNER JOIN instagram_item_viewer v ON v.item_id = p.id
AND v.created >= '2014-08-01'
WHERE s.expirydate IS NULL
AND s.isLocked = 0
GROUP BY s.username
Give thata shot.
As mentioned by #Lennart, if you have a sample data it would be helpful. Because otherwise there will be assumptions.
Try run this to debug (this is not the answer yet)
SELECT s.username, p.id, COUNT( v.id ) AS cnt
FROM `instagram_item_viewer` v
INNER JOIN `instagram_shop_picture` p ON v.item_id = p.id
INNER JOIN `instagram_shop` s ON p.shop_id = s.id
AND s.expirydate IS NULL
AND s.isLocked =0
AND v.created >= '2014-08-01'
GROUP BY (
s.username, p.id
)
ORDER BY cnt DESC
The problem here is the store and item viewer is too far apart (i.e. bridged via shop_picture). Thus shop_picture needs to be in the SELECT statement.
Your original query only gets the first shop_picture count for that store that is why it is less than expected
Ultimately if you still want to achieve your goal, you can expand my SQL above to
SELECT x.username, SUM(x.cnt) -- or COUNT(x.cnt) depending on what you want
FROM
(
SELECT s.username, p.id, COUNT( v.id ) AS cnt
FROM `instagram_item_viewer` v
INNER JOIN `instagram_shop_picture` p ON v.item_id = p.id
INNER JOIN `instagram_shop` s ON p.shop_id = s.id
AND s.expirydate IS NULL
AND s.isLocked =0
AND v.created >= '2014-08-01'
GROUP BY (
s.username, p.id
)
ORDER BY cnt DESC
) x
GROUP BY x.username

sql select distinc where max date

I have 3 tables "maintenances", "cars", "users" . I want to select all data from table maintenance with a distinct car_id and the last record for each distinct (based on max maintenance_date)
SELECT
m. * , u.username, c.Model, c.Make, c.License, c.Milage, COUNT( m.process_id ) AS count_nr
FROM
maintenances AS m
LEFT JOIN users AS u ON u.id = m.user_id
LEFT JOIN cars AS c ON c.id = m.car_id
WHERE
maintenance_date = (SELECT MAX(maintenance_date) FROM maintenances WHERE car_id = m.car_id)
The problem is that this query returns only one record which has the max date from all records. I want all records (distinct car_id and from records with the same car_id to display only values for max(maintenance_date))
This is your query:
SELECT m. * , u.username, c.Model, c.Make, c.License, c.Milage, COUNT( m.process_id ) AS count_nr
----------------------------------------------------------------^
FROM maintenances AS m LEFT JOIN
users AS u
ON u.id = m.user_id LEFT JOIN
cars AS c
ON c.id = m.car_id
WHERE maintenance_date = (SELECT MAX(maintenance_date) FROM maintenances WHERE car_id = m.car_id);
It is an aggregation query. Without a group by, only one row is returned (all the rows are in one group). So, add the group by:
SELECT m. * , u.username, c.Model, c.Make, c.License, c.Milage, COUNT( m.process_id ) AS count_nr
FROM maintenances AS m LEFT JOIN
users AS u
ON u.id = m.user_id LEFT JOIN
cars AS c
ON c.id = m.car_id
WHERE maintenance_date = (SELECT MAX(m2.maintenance_date) FROM maintenances m2 WHERE m2.car_id = m.car_id);
GROUP BY c.id
I also fixed the correlation statement, to be clear that it is correlated to the outer query.
add GROUP BY u.username .
WHERE
maintenance_date = (SELECT MAX(maintenance_date) FROM maintenances WHERE car_id = m.car_id)
GROUP BY u.username

Select row with max value in one column

I have a select statement that returns two columns: office names and total per office:
select o.OfficeName, c.Total
from Offices o
left join
( select OfficeID, count(*) Total
from Customers c
group by OfficeID
) c on o.OfficeID = c.OfficeID
where o.ClusterID = 29
How can I get the row that has max total?
"Customers" table has an "OfficeID" colummn. For a given "ClusterID", I select all offices within the cluster identified by cluster id (e.g. 29) and count the customers belongin to those offices.
There are a number of approaches:
SELECT OfficeName, Total
FROM ( SELECT o.OfficeName, c.Total, MAX(Total) OVER() [MaxTotal]
FROM Offices o
LEFT JOIN
( SELECT OfficeID, COUNT(*) Total
FROM Customers
GROUP BY OfficeID
) c
ON o.OfficeID = c.OfficeID
WHERE o.ClusterID = 29
) c
WHERE Total = MaxTotal
OR
WITH CTE AS
( SELECT o.OfficeName, c.Total
FROM Offices o
LEFT JOIN
( SELECT OfficeID, COUNT(*) Total
FROM Customers
GROUP BY OfficeID
) c
ON o.OfficeID = c.OfficeID
WHERE o.ClusterID = 29
)
SELECT *
FROM CTE
WHERE Total = (SELECT MAX(Total) FROM CTE)
OR
SELECT TOP 1 o.OfficeName, c.Total
FROM Offices o
LEFT JOIN
( SELECT OfficeID, COUNT(*) Total
FROM Customers
GROUP BY OfficeID
) c
ON o.OfficeID = c.OfficeID
WHERE o.ClusterID = 29
ORDER BY Total DESC
Although using TOP 1 may not be what you are after, with the other methods if there are 2 offices with the same number of customers they will both be returned, whereas TOP 1 will only return 1 of these (probably in order of office name). If you only ever want 1 record, then this is the best method
SELECT TOP 1 o.OfficeName, c.Total
FROM Offices o
LEFT JOIN
(SELECT OfficeID, count(*) Total
FROM Customers c
GROUP BY OfficeID
) c ON o.OfficeID = c.OfficeID
WHERE o.ClusterID = 29
ORDER BY c.Total DESC
WITH TIES offers a cleaner way to get all offices sharing the top count:
with a as (
select o.OfficeID,Total=COUNT(*)
from Offices o
inner join Customers c on c.OfficeID=o.OfficeID
group by o.OfficeID
)
select top 1 WITH TIES t.OfficeName, a.Total
from a
inner join Offices t on t.OfficeID=a.OfficeID
where t.ClusterID=29
order by a.Total desc