我找到了一些非常有帮助的解决方案one in particular,几乎让我在那里,但是我坚持到最后一点。我现在需要做的就是能够指定要显示的CarNumbers的范围。
SQL可能更有意义......
SELECT sr.* FROM StatusReport sr
INNER JOIN ( SELECT CarNumber, MAX(StatusReportTime) AS MaxDateTime
FROM StatusReport GROUP BY CarNumber ) groupedsr ON sr.CarNumber =
groupedsr.CarNumber AND sr.StatusReportTime = groupedsr.MaxDateTime
我基本上希望能够添加以下行。
WHERE CarNumber BETWEEN '0' AND '3999'
有人可以帮帮我吗?
提前致谢,
麦克
答案 0 :(得分:1)
您可以在外部查询的末尾添加它,或者在GROUP BY
子句之前的子查询末尾添加它,如下所示:
SELECT sr.*
FROM StatusReport sr
INNER JOIN
(
SELECT CarNumber, MAX(StatusReportTime) AS MaxDateTime
FROM StatusReport
WHERE CarNumber BETWEEN '0' AND '3999'
GROUP BY CarNumber
) groupedsr ON sr.CarNumber = groupedsr.CarNumber
AND sr.StatusReportTime = groupedsr.MaxDateTime;
答案 1 :(得分:0)
SELECT sr.*
FROM StatusReport sr
INNER JOIN
(SELECT CarNumber,
MAX(StatusReportTime) AS MaxDateTime
FROM StatusReport
WHERE CarNumber >0
GROUP BY CarNumber) groupedsr ON sr.CarNumber = groupedsr.CarNumber
AND sr.StatusReportTime = groupedsr.MaxDateTime
答案 2 :(得分:0)
你可以这样做..这将很容易理解并简化你的查询
with tbl2 as
(
select tbl1.*,
row_number() over (order by CarNumber) rtn
from
(select CarNumber, max(StatusReportTime) as MaxDateTime from [StatusReport] group by
CarNumber) as tbl1
)
select * from tbl2 where rtn between 0 and 3999