mySQL query - show most popular item

前端 未结 3 698
渐次进展
渐次进展 2021-02-11 04:53

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

相关标签:
3条回答
  • 2021-02-11 05:17

    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.

    0 讨论(0)
  • 2021-02-11 05:17

    This is as close as I could get....

    SELECT DISTINCT p.date, ItemTotalsByDate.Item, DateTotals.Total
        FROM test_popularity p
        INNER JOIN 
    (SELECT date, MAX(cnt) DayMax from
    (SELECT date, item, COUNT(*) cnt
    FROM dbo.test_popularity
    GROUP BY date, item) tbl
    GROUP BY date) MaxesByDate
        ON p. date = MaxesByDate.date
    INNER JOIN 
    (SELECT date, item, COUNT(*) Total FROM dbo.test_popularity
    GROUP BY date, item) ItemTotalsByDate
        ON MaxesByDate.date = ItemTotalsByDate.date AND MaxesByDate.DayMax = ItemTotalsByDate.Total
    INNER JOIN
    (SELECT date, COUNT(*) Total FROM dbo.test_popularity
    GROUP BY date) DateTotals
    ON p.date = DateTotals.date
    

    The only thing this leaves for your PHP to do is only display the first result it finds for a given date. I couldn't figure out a good way to arbitrarily pick one item when it was a tie. Hope this helps.

    0 讨论(0)
  • 2021-02-11 05:27

    thanks to hohodave for his initial response:

    SELECT date, item, cnt, (
    SELECT COUNT( * )
    FROM test_popularity
    WHERE date = t.date
    ) AS totalCnt
    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;
    
    0 讨论(0)
提交回复
热议问题