如何在一个时间范围内只记录一次记录?

时间:2012-01-27 13:14:11

标签: mysql sql database

我有统计数据表。

我正在尝试计算按版本分组的记录,问题是记录有另一个标准(ref),并且应该只计算一次(每个参考)。

统计数据样本数据来说明问题:

id stat_date  ref version
-------------------------
1  2012-01-25   1  A
2  2012-01-25   2  B
3  2012-01-25   3  A
4  2012-01-26   8  B
5  2012-01-26   2  B
6  2012-01-26   3  B  <-- version has been updated for ref=3

简单计数将返回

SELECT COUNT(*),version FROM stat GROUP BY version

1,A
5,B

这里的问题是只应计算ref = 3(id = 6)的最后一条记录,并且必须忽略(id = 3)。 所以问题是,如何从查询中过滤行(id = 3)?

我无法弄清楚我应该在子查询中插入什么条件

SELECT COUNT(*),version FROM stats
WHERE  stat_date BETWEEN "2012-01-25"  AND "2012-01-26" 
AND id = (SELECT MAX(id) FROM stats WHERE <condition>)
GROUP BY 2

预期结果将是:

1,A (since id=3 is ignored)
3,B (since the first id=2 is ignored and only id=5 is taken into account)

3 个答案:

答案 0 :(得分:2)

忽略哪一行无关紧要。你想要的只是计算一次ref。

SELECT COUNT(DISTINCT ref),version FROM stat GROUP BY version

编辑:

根据你的逻辑,你的例子也应该计算一次ref = 2的2行。

这个答案可以解决所提出的问题,即使它没有解决你未知的真实问题

答案 1 :(得分:1)

尝试:

SELECT COUNT(*),version 
FROM stats s1
WHERE stat_date BETWEEN "2012-01-25"  AND "2012-01-26" AND
      not exists (SELECT null 
                  FROM stats s2 
                  WHERE s1.ref = s2.ref and s2.id>s1.id)
GROUP BY version

编辑:不保证始终有效:

SELECT COUNT(*),version from
(select * from
 (select * from stats 
  where stat_date BETWEEN "2012-01-25"  AND "2012-01-26" 
  order by ref, id desc) s0
 group by ref) s1
group by version

答案 2 :(得分:0)

SELECT COUNT(*),version FROM stats
   WHERE  stat_date BETWEEN "2012-01-25"  AND "2012-01-26" 
       AND id = (SELECT MAX(s.id) FROM stats s WHERE s.id = id)
   GROUP BY version
   ORDER BY stat_date