如何在SQL Server中按时间间隔显示数据方名称
为这个大帖子道歉 我想显示数据方名称和时间间隔。这里附上一个屏幕截图,这样人们就可以了解我想要什么样的输出 这是我的表,我将从中获取数据。所以在这里粘贴表数据如何在SQL Server中按时间间隔显示数据方名称,sql,sql-server,Sql,Sql Server,为这个大帖子道歉 我想显示数据方名称和时间间隔。这里附上一个屏幕截图,这样人们就可以了解我想要什么样的输出 这是我的表,我将从中获取数据。所以在这里粘贴表数据 Call start Call duration Ring duration Direction Is_Internal Continuation Party1Name Park_Time ------------------------- ---------------- --------
Call start Call duration Ring duration Direction Is_Internal Continuation Party1Name Park_Time
------------------------- ---------------- ------------- --------- ----------- ------------ --------------- -----------
2015/06/08 08:06:08 00:02:28 2 I 0 0 Emily 0
2015/06/08 08:16:38 00:00:21 0 I 0 1 Line 2.0 0
2015/06/08 08:16:38 00:04:13 5 I 0 0 Jen 0
2015/06/08 08:21:13 00:00:21 0 I 0 1 Line 3.0 0
2015/06/08 08:21:40 00:00:21 0 I 0 1 Line 2.0 0
2015/06/08 08:22:14 00:00:21 0 I 0 1 Line 4.0 0
2015/06/08 08:21:13 00:01:28 8 I 0 0 Angela 0
2015/06/08 08:21:40 00:03:28 9 I 0 0 Emily 0
2015/06/08 08:22:14 00:03:51 6 I 0 0 Jen 0
2015/06/08 08:29:30 00:00:21 0 I 0 1 Line 2.0 0
2015/06/08 08:31:12 00:00:00 0 O 0 0 Tom 0
2015/06/08 08:31:18 00:00:54 0 O 0 0 Tom 0
2015/06/08 08:29:30 00:01:30 13 I 0 1 Tom 0
我设法形成sql以获取所需的数据。下面是一个示例sql,它返回数据
SELECT
Party1Name,
DATEADD(minute, DATEDIFF(minute,0,[Call Start]) / 30 * 30, 0) AS "DateStart",
DATEADD(minute, 30, DATEADD(minute, DATEDIFF(minute, 0, [Call Start]) / 30 * 30, 0)) AS "DateEnd",
SUM(CASE WHEN Direction = 'I' THEN 1 ELSE 0 END) AS "Incoming",
SUM(CASE WHEN Direction = 'O' THEN 1 ELSE 0 END) AS "Outgoing",
SUM(CASE WHEN Direction = 'T' THEN 1 ELSE 0 END) AS "Transfer"
FROM
CSRPhoneData_Interval
GROUP BY
Party1Name,
DATEADD(minute, DATEDIFF(minute,0,[Call Start]) / 30 * 30, 0)
ORDER BY
Party1Name,
DATEADD(minute, DATEDIFF(minute,0,[Call Start]) / 30 * 30, 0)
上面的查询输出如下
Party1Name DateStart DateEnd Incoming Outgoing Transfer
-------------- ----------------------- ----------------------- ----------- ----------- -----------
Accounting 2015-06-08 10:30:00.000 2015-06-08 11:00:00.000 0 1 0
Accounting 2015-06-08 14:30:00.000 2015-06-08 15:00:00.000 0 1 0
Accounting 2015-06-08 16:30:00.000 2015-06-08 17:00:00.000 1 0 0
Andrea 2015-06-08 09:30:00.000 2015-06-08 10:00:00.000 1 0 0
Andrea 2015-06-08 10:00:00.000 2015-06-08 10:30:00.000 2 2 0
Andrea 2015-06-08 10:30:00.000 2015-06-08 11:00:00.000 8 5 0
Andrea 2015-06-08 11:00:00.000 2015-06-08 11:30:00.000 5 1 0
Andrea 2015-06-08 11:30:00.000 2015-06-08 12:00:00.000 8 3 0
Andrea 2015-06-08 12:00:00.000 2015-06-08 12:30:00.000 7 0 0
Andrea 2015-06-08 12:30:00.000 2015-06-08 13:00:00.000 5 3 0
Andrea 2015-06-08 13:30:00.000 2015-06-08 14:00:00.000 3 2 0
Andrea 2015-06-08 14:00:00.000 2015-06-08 14:30:00.000 4 4 0
Andrea 2015-06-08 14:30:00.000 2015-06-08 15:00:00.000 6 5 0
Andrea 2015-06-08 15:00:00.000 2015-06-08 15:30:00.000 1 3 0
Andrea 2015-06-08 15:30:00.000 2015-06-08 16:00:00.000 6 0 0
Andrea 2015-06-08 16:00:00.000 2015-06-08 16:30:00.000 4 3 0
Andrea 2015-06-08 16:30:00.000 2015-06-08 17:00:00.000 3 4 0
Andrea 2015-06-08 17:00:00.000 2015-06-08 17:30:00.000 3 2 0
Andrea 2015-06-08 17:30:00.000 2015-06-08 18:00:00.000 4 1 0
Andrea 2015-06-08 18:00:00.000 2015-06-08 18:30:00.000 4 0 0
Angela 2015-06-08 08:00:00.000 2015-06-08 08:30:00.000 1 0 0
Angela 2015-06-08 08:30:00.000 2015-06-08 09:00:00.000 5 0 0
Angela 2015-06-08 09:00:00.000 2015-06-08 09:30:00.000 8 0 0
Angela 2015-06-08 09:30:00.000 2015-06-08 10:00:00.000 3 3 0
Angela 2015-06-08 10:00:00.000 2015-06-08 10:30:00.000 3 0 0
Angela 2015-06-08 10:30:00.000 2015-06-08 11:00:00.000 5 1 0
Angela 2015-06-08 11:00:00.000 2015-06-08 11:30:00.000 4 1 0
Angela 2015-06-08 11:30:00.000 2015-06-08 12:00:00.000 7 1 0
Angela 2015-06-08 12:00:00.000 2015-06-08 12:30:00.000 4 2 0
问题是我必须按时间间隔显示数据,就像各方数据应该显示从9:00:00-9:30:00到17:30:00
的时间间隔数据一样
所以我创建了一个用于返回时间间隔的CTE
DECLARE @StartTime datetime = '2015-06-09 09:00:00',
@EndTime datetime = '2015-06-09 18:00:00',
@Interval int = 30 -- this can be changed.
;WITH cSequence AS
(
SELECT
@StartTime AS StartRange,
DATEADD(MINUTE, @Interval, @StartTime) AS EndRange
UNION ALL
SELECT
EndRange,
DATEADD(MINUTE, @Interval, EndRange)
FROM cSequence
WHERE DATEADD(MINUTE, @Interval, EndRange) < @EndTime
)
用于输出数据计算
用于输出数据计算
如果可能,请帮助我修改代码。谢谢你为什么要在DB级别上这么做?很明显,这是一个表示问题,这是应该处理的地方(可能更容易处理)。我的限制是,我必须在db级别执行此操作,并将数据返回到UI,在UI中绑定到网格以进行显示。“现在我无法将此CTE与我的表交叉连接,以获得数据方名称和时间间隔信息。”为什么不这样做?对我来说似乎是一个简单的结合。也许你需要外敷?
where direction='I' and
Is_Internal=0 and continuation=0 and
RIGHT(convert(varchar,[call duration]),8)<> '00:00:00'
where direction='O'
and Is_Internal=0 and continuation=0
where continuation=1
and RIGHT(convert(varchar,[call duration]),8)<> '00:00:00'
where direction='I' and
RIGHT(convert(varchar,[call duration]),8)= '00:00:00' and [Ring duration]>0