我从带有25个列标题的百万行表中提取数据。我有一个简单的查询,可以按日期提取已接听的总呼叫数和总连接数。
SELECT
convert(date,format(Dateadd(hh,1,[Start Time]),'dd/MM/yyyy'),103) as [Date]
,sum(iif([type] = 'normal operator call',1,0)) as [answered calls]
,sum([connected (secs)]) as [total con sec]
from inboundcallsview
where [account] = '106844'
and dateadd(hh,1,[start time]) between '2017-05-01' and '2017-06-01'
group by convert(date,format(Dateadd(hh,1,[Start Time]),'dd/MM/yyyy'),103)
我要做的是将前300个(通过[开始时间]列)已接听电话的连接秒数([type] ='正常的操作员电话')的任何一行相加每天(由[日期]栏定义)。以及从301开始的所有呼叫的总连接秒数。基本上我的当前输出是
我希望它是
忽略日期的变化6月(06)应该是(05)我搞砸了创建我的例子。我最终会想要在所有日期都这样做,所以日期参数并不重要。
提前致谢。
答案 0 :(得分:1)
在psuedo代码中,它将如下所示:
WITH CTE AS (
SELECT {Your Date Output} AS [date],
,[ConnectedSecs]
ROW_NUMBER() OVER (PARTITION BY {Your Date Output}, ORDER BY [Start Time]) AS rn
FROM YourTable
WHERE {Conditions}
)
SELECT
[date],
'First 300' AS Bracket,
SUM(ConnectedSecs)
FROM CTE WHERE rn < 301
GROUP BY [date]
UNION ALL SELECT
[date],
'301+' AS Bracket,
SUM(ConnectedSecs)
FROM CTE WHERE rn >= 301
GROUP BY [date]
ORDER BY [date],{a case expression to have 'First 300' on top if desired}