我有一张表,其中包含每个电话的记录。 我想创建一个矩阵,其中我每天30分钟的时间段内的平均呼叫率是一天中的一天。每个通话都有一个开始时间,即日期/时间
即
我希望将Mon-Sun显示为行标题,将30分钟间隔显示为列标题。内容是每个特定30分钟段中每天(周一至周日)的平均呼叫数。这是我所拥有的,但它不是我想要的。它按时间段计算呼叫数量,我无法计算出如何获得平均值。
我希望有些人可以指出我正确的方向 -
SQL不是我经常使用的东西所以要温和: - )
set dateformat dmy
SET DATEFIRST 1 -- Start with Monday
SELECT datepart(dw,starttime) as 'Day',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS 'Midnight-12:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '12:30am-1am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '1am-1:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '1:30am-2am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '2am-2:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '2:30am-3am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '3am-3:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '3:30am-4am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '4am-4:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '4:30am-5am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '5am-5:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '5:30am-6am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '6am-6:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '6:30am-7am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '7am-7:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '7:30am-8am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '8:30am-9am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '9am-9:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '10:30am-11am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '11am-11:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '11:30am-Noon',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS 'Noon-12:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '1pm-1:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '1:30pm-2pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '2pm-2:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '2:30pm-3pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '3pm-3:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '4:30pm-4pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '4pm-5:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '5:30pm-6pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '6pm-6:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '6:30pm-7pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '7pm-7:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '7:30pm-8pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '8pm-8:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '8:30pm-9pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '9pm-9:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '9:30pm-10pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '10pm-10:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '10:30pm-11pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '11pm-11:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '11:30pm-Midnight'
FROM [dbo].[CallList]
where StartTime>'1/6/2015'
and StartTime<'30/6/2015'
GROUP BY datepart(dw,starttime)
ORDER BY datepart(dw,starttime)
答案 0 :(得分:0)
创建一个CREATE TABLE #Data
(
ID UNIQUEIDENTIFIER
,DateIn DATETIME
)
INSERT INTO #Data
VALUES
(NEWID(), '2016-05-17 00:01:00'),
(NEWID(), '2016-05-17 00:21:00'),
(NEWID(), '2016-05-17 01:01:00'),
(NEWID(), '2016-05-17 05:41:00'),
(NEWID(), '2016-05-17 06:51:00'),
(NEWID(), '2016-05-17 13:21:00'),
(NEWID(), '2016-05-17 18:01:00'),
(NEWID(), '2016-05-17 21:11:00'),
(NEWID(), '2016-05-17 23:41:00'),
(NEWID(), '2016-05-17 23:51:00')
DECLARE @Start DATETIME
DECLARE @End DATETIME
SET @Start = '2016-05-17 00:00:00'
SET @End = '2016-05-17 00:30:00'
CREATE TABLE #Temp
(
ID UNIQUEIDENTIFIER
,[Time Increment] VARCHAR(100)
)
WHILE @End <= '2016-05-17 23:30:00'
BEGIN
INSERT INTO #Temp
SELECT
ID
,CAST(@Start AS VARCHAR) + ' - ' + CAST(@End AS VARCHAR) [Time Increment]
FROM #Data
WHERE DateIn >= @Start
AND DateIn < @End
SET @Start = DATEADD(Minute, 30, @Start)
SET @End = DATEADD(Minute, 30, @End)
END
SELECT
[Time Increment]
,COUNT(ID) [Count]
FROM #Temp
GROUP BY [Time Increment]
DROP TABLE #Temp
DROP TABLE #Data
循环以插入临时表,然后在事实之后进行聚合是一个可行的选项。见下文:
Time Increment |Count
May 17 2016 1:00AM - May 17 2016 1:30AM | 1
May 17 2016 1:00PM - May 17 2016 1:30PM | 1
May 17 2016 5:30AM - May 17 2016 6:00AM | 1
May 17 2016 6:00PM - May 17 2016 6:30PM | 1
May 17 2016 6:30AM - May 17 2016 7:00AM | 1
May 17 2016 9:00PM - May 17 2016 9:30PM | 1
May 17 2016 12:00AM - May 17 2016 12:30AM | 2
在此示例中,结果如下:
var pageData = new Observable();
pageData.set('customObject', {"label" : 'This is my Label'});
pageData.set('someString', "my random string");
exports.onLoaded = function(args) {
var page = args.object;
page.bindingContext = pageData;
}