I need to find the most popular occurrence of an item grouped by date and display the total of all items along with the name of this item. Is something like this possible in a s
My initial suggestion was incorrect:
SELECT
date, item, SUM(cnt)
FROM (
SELECT
date, item, count(item_id) AS cnt
FROM test_popularity
GROUP BY date, item_id
ORDER BY cnt DESC
) t
GROUP BY date;
This erroneously assumes that the outside aggregation (by date) will select the first row of the inner derived table which was ordered by cnt. This behavior is, in fact, undefined and not guaranteed to be consistent.
Here is the proper solution:
SELECT
t1.date, t1.item,
(SELECT COUNT(*) FROM test_popularity WHERE date = t1.date) as total
# see note!
FROM test_popularity t1
JOIN (
SELECT date, item, item_id, COUNT(item_id) as count
FROM test_popularity
GROUP BY date, item_id
) AS t2
ON t1.date = t2.date AND t1.item_id = t2.item_id
GROUP BY t1.date;
Note:
I added the (SELECT COUNT(*)) AS total
because the question asked for this in one query. However, this will not scale as it is a correlated subquery. This means that for every t1.date the SELECT COUNT(*) subquery will run. Please benchmark and see if it performs suitably for your needs. If not, then I suggest getting the daily totals in a separate query. You would merge these results in your application.