我有一个场景,我需要根据最新日期对ID进行不同的计数。
ID OccurredOn
1717 2017-07-21 08:56:01.553
1719 2017-07-21 08:42:21.377
1719 2017-07-04 08:51:49.827
1719 2017-07-04 08:51:11.670
请建议。谢谢!
答案 0 :(得分:0)
我认为您需要每个ID的最大日期,以便在以下内容中使用 void foodRandPos(Sprite & spriteFood, int blockDimensionXY, int foodDimensionXY, int windowSi)
和MAX
:
GROUP BY
因此输出将如下:
SELECT ID, MAX(OccuredOn) AS OccuredOn
FROM tbl
GROUP BY ID
另一种想法是,如果你想在SQL-Server中获取最新一天的ID,你可以尝试这样的事情:
ID OccurredOn
1717 2017-07-21 08:56:01.553
1719 2017-07-04 08:51:49.827
因此输出将是:
SELECT ID, MAX(OccuredOn) AS OccuredOn, MAX(cnt) AS cnt
FROM (
SELECT
ID,
ROW_NUMBER() OVER(PARTITION BY ID, CAST(OccuredOn AS DATE) ORDER BY OccuredOn ASC) AS cnt,
CAST(OccuredOn AS DATE)AS OccuredOn
FROM tbl
)x
GROUP BY ID
<强>更新强>
在子查询中,您可以将datetime列转换为日期,为每个ID获取最大日期。之后选择每个不同ID的计数。请尝试以下方法:
ID OccuredOn cnt
1717 2017-07-21 1
1719 2017-07-21 1