我有一张桌子。我想获得每个独特的CoatingChambersID的最后一行。有24个独特的腔室,最后一行表示腔室处于最后状态的时间。
我可以得到一个如此独特的列表:
SELECT distinct CoatingChambersID, MAX(LastDT) as LastDT
FROM [REO].[dbo].[CoatingChamberStateLogs]
group by CoatingChambersID
但我无法弄清楚如何将其加入原始表格。
提前感谢您的帮助!
CoatingChamberStateLogsID CoatingChambersID StartDT LastDT IntervalMin CoatingChamberStatesID
1001712 1003 2017-01-24 23:09:29.4632108 2017-01-25 03:04:32.6758500 235.053543986667 1007
1001713 1006 2017-01-24 23:09:29.4632108 2017-01-25 00:30:30.8478433 81.0230772083333 1007
1001714 1015 2017-01-24 23:09:29.4632108 2017-01-25 04:21:33.7601011 312.071614838333 1007
1001715 1024 2017-01-24 23:09:29.4632108 2017-01-31 04:43:21.5643016 8973.86835151333 1001
1001716 1016 2017-01-24 23:09:29.4632108 2017-01-25 00:01:30.4200122 52.01594669 1006
1001717 1017 2017-01-24 23:09:29.4632108 2017-01-24 23:15:29.8261612 6.00604917333333 1006
1001718 1018 2017-01-24 23:09:29.4632108 2017-01-26 01:42:49.4040548 1593.3323474 1006 0
1001719 1019 2017-01-24 23:09:29.4632108 2017-01-25 02:25:32.3047026 196.047358196667 1005
1001720 1020 2017-01-24 23:09:29.4632108 2017-01-24 23:12:29.8009482 3.00562895666667 1007
1001721 1022 2017-01-24 23:09:29.4632108 2017-01-25 02:52:32.5995144 223.052271726667 1007
1001722 1023 2017-01-24 23:09:29.4632108 2017-01-25 03:05:32.9236594 236.057674143333 1007
1001723 1002 2017-01-24 23:09:29.4475820 2017-01-25 02:14:32.1723891 185.045413451667 1001
1001724 1004 2017-01-24 23:09:29.4632108 2017-01-25 00:39:31.0878194 90.02707681 1001
1001725 1005 2017-01-24 23:09:29.4632108 2017-01-24 23:18:29.8783565 9.006919095 1001
答案 0 :(得分:6)
使用row_number()
:
select ccs.*
from (select ccs.*,
row_number() over (partition by ccs.CoatingChambersID order by ccs.LastDt desc) as seqnum
from [REO].[dbo].[CoatingChamberStateLogs] ccs
) ccs
where seqnum = 1;
select distinct
与group by
的使用几乎永远不正确。
编写查询的另一种有趣方式并不需要子查询:
select top (1) with ties ccs.*
from [REO].[dbo].[CoatingChamberStateLogs] ccs
order by row_number() over (partition by ccs.CoatingChambersID order by ccs.LastDt desc);
答案 1 :(得分:0)
虽然我喜欢戈登的答案......我想提供一个辅助选择。 我没有花时间确定交叉申请是否会更有效率;但考虑到加入的成本,我认为他可能更有效率。但是没有测试我就不能说了。
SELECT B.*
FROM [REO].[dbo].[CoatingChamberStateLogs] A
CROSS APPLY (SELECT TOP 1 *
FROM [REO].[dbo].[CoatingChamberStateLogs] B
WHERE A.CoatingChambersID = B.CoatingChambersID
ORDER BY lastDT Desc)
我知道它在1-M关系中的许多前n个记录中都运行良好,但由于这是一个自我加入,他的响应可能会导致更好的执行计划,因为它避免了连接。