3
    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
4

3 回答 3

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
于 2013-04-09T14:38:04.440 回答
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
于 2013-04-09T14:52:43.623 回答
0

试试这个:

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

(从 dbo.Calls 中选择前 1 个 dbo.Calls.QueryTime,其中 dbo.Calls.StartTime = CLL.StartTime order by dbo.Calls.QueryTime desc)

group by CLL.CallID,CLL.StartTime,CLL.EndTime,CLL.QueryTime
order by CLL.StartTime -- 你可以拒​​绝这行

于 2013-04-10T19:50:00.687 回答