Max sql server没有group by

时间:2013-04-09 14:36:17

标签: sql-server tsql group-by

    CallID  StartTime           EndTime                 Querytime
    1692    2012-11-20 11:52:00.000 2012-11-20 11:52:00.300 0.300
    1693    2012-11-20 11:52:00.000 2012-11-20 11:52:00.100 0.100
    1694    2012-11-20 11:52:00.000 2012-11-20 11:52:00.400 1.5
    1695    2012-11-20 11:52:01.000 2012-11-20 11:52:01.400 3
    1696    2012-11-20 11:52:01.000 2012-11-20 11:52:01.300 5

我想获得由StartTime分组的最大查询时间,如下所示,但我仍然希望显示CallID。

     StartTime                  MaxQueryTime
     2012-11-11 19:04:07.000    0.300
     2012-11-11 19:04:10.000    0.200
     2012-11-11 19:08:48.000    0.300
     2012-11-11 19:08:51.000    0.300
     2012-11-11 19:09:27.000    0.100

     SELECT     StartTime, MAX(Querytime) AS QueryTime
     FROM         dbo.Calls
     GROUP BY StartTime

3 个答案:

答案 0 :(得分:2)

WITH records
AS
(
    SELECT  CallID, StartTime, EndTime, QueryTime,
            DENSE_RANK() OVER (ORDER BY QueryTime DESC) rn
    FROM    TableName
)
SELECT CallID, StartTime, EndTime, QueryTime
FROM records
WHERE rn = 1

答案 1 :(得分:0)

select CallID, StartTime, max(QueryTime) over (partition by StartTime) as QueryTime
from (
    SELECT CallID, StartTime, MAX(Querytime) as QueryTime
    FROM dbo.Calls
    GROUP BY CallID, StartTime
 ) t

答案 2 :(得分:0)

试试这个:

  

选择CallID,StartTime,EndTime,QueryTime
  来自dbo.Calls作为CLL
  其中CLL.QueryTime =

     
    

(从dbo.Calls中选择前1个dbo.Calls.QueryTime,其中dbo.Calls.StartTime = CLL.StartTime按dbo.Calls.QueryTime desc排序)

  
     

按CLL.CallID分组,CLL.StartTime,CLL.EndTime,CLL.QueryTime
  按CLL.StartTime排序 - 你可以拒​​绝这一行