用于获取最新日期记录的Groupwise最大查询

时间:2012-11-19 10:03:24

标签: mysql aggregate-functions

我正在尝试构建一个只根据日期获取一组记录的最新记录的查询。

表的布局如下:

| date | category | action | label | label2 | count_today | count_total | period |

主键基于列datecategoryactionlabellabel2perioddate的格式为yyyy-mm-dd,期间的值可以为DayWeekmonth

对于category | action | label | label2的每个唯一组合,我需要记录最新日期。

我的第一次尝试是:

SELECT * FROM `statistic` 
WHERE 
 (action='total' OR action='' OR category='user')
 AND
 (period='day' 
   OR (period='week' AND DATEDIFF(now(), `date`) > 30)
   OR (period = 'Month' AND DATEDIFF(now(), `date`) > 7*26)
 )
GROUP BY category, action, label, label2
ORDER BY date DESC

此查询的问题在于它在ORDER BY之前执行GROUP BY,导致返回不正确的记录。

搜索后,我发现我想要的是group-wise maximum查询。

我的下一次尝试是:

SELECT s1.* FROM `statistic` AS s1
LEFT JOIN statistic AS s2 
ON 
 s1.category = s2.category
 AND s1.action = s2.action
 AND s1.label = s2.label
 AND s1.label2 = s2.label2
 AND s1.date > s2.date
WHERE 
   (s1.action='total' OR s1.action='' OR s1.category='user')
   AND
   (s1.period='day' 
   OR (s1.period='week' AND DATEDIFF(now(), s1.`date`) > 30)
   OR (s1.period = 'Month' AND DATEDIFF(now(), s1.`date`) > 7*26)
 )
GROUP BY category, action, label, label2

但是这个查询也没有给我正确的结果(它看起来与第一个查询类似)。

我是如何得到我需要的数据的?

1 个答案:

答案 0 :(得分:4)

你是对的,你想要group-wise maximum,但是你可以通过将你的表与一个查找每个组的最新日期的子查询结合来实现这一目标:

SELECT * FROM statistic NATURAL JOIN (
  SELECT   category, action, label, label2, MAX(date) date
  FROM     statistic
  GROUP BY category, action, label, label2
) t

然后,如果仍然需要以下过滤器:

WHERE 
 (action='total' OR action='' OR category='user')
 AND
 (period='day' 
   OR (period='week' AND DATEDIFF(now(), `date`) > 30)
   OR (period = 'Month' AND DATEDIFF(now(), `date`) > 7*26)
 )