2

我找到了一些特别有帮助的解决方案,几乎让我到达那里,但我坚持最后一点。我现在需要做的就是能够指定要显示的 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' 

谁能帮帮我?

提前致谢,

麦克风

4

3 回答 3

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;
于 2013-01-06T07:31:53.353 回答
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
于 2013-01-06T07:32:15.003 回答
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
于 2013-01-06T07:54:28.190 回答