为什么我的透视SQL会返回所有NULL值?

时间:2017-02-15 17:03:48

标签: null pivot

我一直在为此烦恼很多个小时,并决定寻求帮助。我只做了另外一个支点,它比这简单。

我要做的是加入两个表中的数据 - 事件和来源,并按季度自2014年1月开始汇总数据。由于我们差不多到2017年的一半 - 第一季度,这将是13列。

我的SQL确实编译,但它为每列返回NULL。

这是我的SQL:

SELECT [ClientName],
       [1] AS [2014 - Q1],
       [2] AS [2014 - Q2],
       [3] AS [2014 - Q3],
       [4] AS [2014 - Q4],
       [5] AS [2015 - Q1],
       [6] AS [2015 - Q2],
       [7] AS [2015 - Q3],
       [8] AS [2015 - Q4],
       [9] AS [2016 - Q1],
       [10] AS [2016 - Q2],
       [11] AS [2016 - Q3],
       [12] AS [2016 - Q4],
       [13] AS [2017 - Q1]
FROM(
SELECT COALESCE(S.S_Name,'') AS [ClientName],
       (CAST(DATEPART(YEAR,I.opened) AS VARCHAR(4)) + CAST(DATEPART(QUARTER,I.opened) AS VARCHAR(1))) AS [Period],
       COUNT(I.incidentno) AS [IncidentCount]
FROM incident I
LEFT OUTER JOIN source S ON I.SourceId = S.S_ID
WHERE S.S_ID = 'OP_Pac_ANZ'
GROUP BY COALESCE(S.S_Name,''), (CAST(DATEPART(YEAR,I.opened) AS VARCHAR(4)) + CAST(DATEPART(QUARTER,I.opened) AS VARCHAR(1)))
)
src
PIVOT (SUM(src.IncidentCount)
FOR [Period] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13])) AS pvt

这些是实际结果:

2014年第一季度 - 2014年第一季度 - 2014年第二季度 - 2014年第三季度 - 2015年第四季度 - 2015年第一季度 - 2015年第二季度 - 2015年第三季度 - 2016年第四季度 - 2016年第一季度 - 2016年第二季度 - 2016年第三季度 - 2017年第四季度 - 第一季度 ANZ NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL

这些是预期结果:

2014年第一季度 - 2014年第一季度 - 2014年第二季度 - 2014年第三季度 - 2015年第四季度 - 2015年第一季度 - 2015年第二季度 - 2015年第三季度 - 2016年第四季度 - 2016年第一季度 - 2016年第二季度 - 2016年第三季度 - 2017年第四季度 - 第一季度 ANZ 8 17 10 4 8 13 19 10 3 NULL 1 NULL NULL

接下来我将继续提供样本数据。

以下是修改后的SQL:

SELECT [ClientName],
       ['20141'] AS [2014 - Q1],
       ['20142'] AS [2014 - Q2],
       ['20143'] AS [2014 - Q3],
       ['20144'] AS [2014 - Q4],
       ['20151'] AS [2015 - Q1],
       ['20152'] AS [2015 - Q2],
       ['20153'] AS [2015 - Q3],
       ['20154'] AS [2015 - Q4],
       ['20161'] AS [2016 - Q1],
       ['20161'] AS [2016 - Q2],
       ['20162'] AS [2016 - Q3],
       ['20163'] AS [2016 - Q4],
       ['20171'] AS [2017 - Q1]
FROM(
SELECT COALESCE(S.S_Name,'') AS [ClientName],
       (CAST(DATEPART(YEAR,I.opened) AS VARCHAR(4)) + CAST(DATEPART(QUARTER,I.opened) AS VARCHAR(1))) AS [Period],
       COUNT(I.incidentno) AS [IncidentCount]
FROM incident I
LEFT OUTER JOIN source S ON I.SourceId = S.S_ID
WHERE I.SourceId = 'OP_Pac_ANZ'
GROUP BY COALESCE(S.S_Name,''), (CAST(DATEPART(YEAR,I.opened) AS VARCHAR(4)) + CAST(DATEPART(QUARTER,I.opened) AS VARCHAR(1)))
)
src
PIVOT (SUM(src.IncidentCount)
FOR [Period] IN (['20141'],['20142'],['20143'],['20144'],['20151'],['20152'],['20153'],['20154'],['20161'],['20162'],['20163'],['20164'],['20171'])) AS pvt

这是创建两个表并插入数据的SQL。

CREATE TABLE incident
(
incidentno nvarchar(255),
sourceid nvarchar(255),
opened datetime
);

CREATE TABLE source
(
s_id nvarchar(255),
S_name nvarchar(255)
);

INSERT INTO source VALUES
('OP_Pac_ANZ', 'ANZ');

INSERT INTO incident
(incidentno, sourceid, opened)
VALUES
('1506110002', 'OP_Pac_ANZ', '2015-06-11 02:36:00.000'),
('1506110100', 'OP_Pac_ANZ', '2015-06-11 21:48:00.000'),
('1506120010', 'OP_Pac_ANZ', '2015-06-12 03:00:00.000'),
('1501260175', 'OP_Pac_ANZ', '2015-01-26 23:34:00.000'),
('1501290278', 'OP_Pac_ANZ', '2015-01-29 19:40:00.000'),
('1502110143', 'OP_Pac_ANZ', '2015-02-11 15:28:00.000'),
('1501090237', 'OP_Pac_ANZ', '2015-01-09 16:52:00.000'),
('1506150148', 'OP_Pac_ANZ', '2015-06-15 23:31:00.000'),
('1506160142', 'OP_Pac_ANZ', '2015-06-16 23:19:00.000'),
('1506170005', 'OP_Pac_ANZ', '2015-06-17 04:10:00.000'),
('1506170113', 'OP_Pac_ANZ', '2015-06-17 20:53:00.000'),
('1507010002', 'OP_Pac_ANZ', '2015-07-01 00:49:00.000'),
('1507010006', 'OP_Pac_ANZ', '2015-07-01 03:36:00.000'),
('1507010012', 'OP_Pac_ANZ', '2015-07-01 05:06:00.000'),
('1507020104', 'OP_Pac_ANZ', '2015-07-02 22:21:00.000'),
('1507030002', 'OP_Pac_ANZ', '2015-07-03 03:01:00.000'),
('1507030003', 'OP_Pac_ANZ', '2015-07-03 03:11:00.000'),
('1507080007', 'OP_Pac_ANZ', '2015-07-08 05:10:00.000'),
('1507080039', 'OP_Pac_ANZ', '2015-07-08 11:11:00.000'),
('1507080124', 'OP_Pac_ANZ', '2015-07-08 23:31:00.000'),
('1507100003', 'OP_Pac_ANZ', '2015-07-10 01:39:00.000'),
('1507120001', 'OP_Pac_ANZ', '2015-07-12 00:02:00.000'),
('1503300232', 'OP_Pac_ANZ', '2015-03-30 21:37:00.000'),
('1405280003', 'OP_Pac_ANZ', '2014-05-28 04:43:00.000'),
('1405280325', 'OP_Pac_ANZ', '2014-05-28 22:22:00.000'),
('1405290007', 'OP_Pac_ANZ', '2014-05-29 04:11:00.000'),
('1405290009', 'OP_Pac_ANZ', '2014-05-29 04:23:00.000'),
('1406030002', 'OP_Pac_ANZ', '2014-06-03 02:07:00.000'),
('1405140050', 'OP_Pac_ANZ', '2014-05-14 08:47:00.000'),
('1405140322', 'OP_Pac_ANZ', '2014-05-14 19:22:00.000'),
('1405140327', 'OP_Pac_ANZ', '2014-05-14 19:42:00.000'),
('1501120369', 'OP_Pac_ANZ', '2015-01-12 19:36:00.000'),
('1504200174', 'OP_Pac_ANZ', '2015-04-20 23:42:00.000'),
('1510080001', 'OP_Pac_ANZ', '2015-10-08 00:59:00.000'),
('1510080128', 'OP_Pac_ANZ', '2015-10-08 16:39:00.000'),
('1510120006', 'OP_Pac_ANZ', '2015-10-12 03:49:00.000'),
('1510120111', 'OP_Pac_ANZ', '2015-10-12 22:32:00.000'),
('1510120114', 'OP_Pac_ANZ', '2015-10-12 23:33:00.000'),
('1510130002', 'OP_Pac_ANZ', '2015-10-13 03:35:00.000'),
('1503040057', 'OP_Pac_ANZ', '2015-03-04 11:10:00.000'),
('1503300005', 'OP_Pac_ANZ', '2015-03-30 02:39:00.000'),
('1403110339', 'OP_Pac_ANZ', '2014-03-11 21:52:00.000'),
('1406150021', 'OP_Pac_ANZ', '2014-06-15 21:18:00.000'),
('1408130367', 'OP_Pac_ANZ', '2014-08-13 22:13:00.000'),
('1506190003', 'OP_Pac_ANZ', '2015-06-19 00:18:00.000'),
('1406290015', 'OP_Pac_ANZ', '2014-06-29 19:59:00.000'),
('1406290016', 'OP_Pac_ANZ', '2014-06-29 20:03:00.000'),
('1410120030', 'OP_Pac_ANZ', '2014-10-12 21:20:00.000'),
('1408010001', 'OP_Pac_ANZ', '2014-08-01 03:13:00.000'),
('1408030020', 'OP_Pac_ANZ', '2014-08-03 23:38:00.000'),
('1408060301', 'OP_Pac_ANZ', '2014-08-06 22:08:00.000'),
('1403180346', 'OP_Pac_ANZ', '2014-03-18 21:29:00.000'),
('1405150357', 'OP_Pac_ANZ', '2014-05-15 22:42:00.000'),
('1405180033', 'OP_Pac_ANZ', '2014-05-18 20:24:00.000'),
('1405190025', 'OP_Pac_ANZ', '2014-05-19 07:31:00.000'),
('1402170070', 'OP_Pac_ANZ', '2014-02-17 11:07:00.000'),
('1407080003', 'OP_Pac_ANZ', '2014-07-08 03:27:00.000'),
('1407130019', 'OP_Pac_ANZ', '2014-07-13 22:26:00.000'),
('1408190333', 'OP_Pac_ANZ', '2014-08-19 21:18:00.000'),
('1410150001', 'OP_Pac_ANZ', '2014-10-15 01:12:00.000'),
('1402240330', 'OP_Pac_ANZ', '2014-02-24 18:40:00.000'),
('1512210004', 'OP_Pac_ANZ', '2015-12-21 01:30:00.000'),
('1512210005', 'OP_Pac_ANZ', '2015-12-21 01:36:00.000'),
('1403310361', 'OP_Pac_ANZ', '2014-03-31 21:21:00.000'),
('1403030003', 'OP_Pac_ANZ', '2014-03-03 02:46:00.000'),
('1403030417', 'OP_Pac_ANZ', '2014-03-03 19:40:00.000'),
('1410290005', 'OP_Pac_ANZ', '2014-10-29 00:38:00.000'),
('1412220320', 'OP_Pac_ANZ', '2014-12-22 21:10:00.000'),
('1506260012', 'OP_Pac_ANZ', '2015-06-26 03:50:00.000'),
('1506290005', 'OP_Pac_ANZ', '2015-06-29 03:11:00.000'),
('1506300145', 'OP_Pac_ANZ', '2015-06-30 21:06:00.000'),
('1506300150', 'OP_Pac_ANZ', '2015-06-30 23:09:00.000'),
('1406180002', 'OP_Pac_ANZ', '2014-06-18 00:43:00.000'),
('1406180003', 'OP_Pac_ANZ', '2014-06-18 00:45:00.000'),
('1406220007', 'OP_Pac_ANZ', '2014-06-22 09:24:00.000'),
('1408280001', 'OP_Pac_ANZ', '2014-08-28 02:13:00.000'),
('1409070033', 'OP_Pac_ANZ', '2014-09-07 20:28:00.000'),
('1409080003', 'OP_Pac_ANZ', '2014-09-08 01:12:00.000'),
('1508100003', 'OP_Pac_ANZ', '2015-08-10 04:10:00.000'),
('1508110004', 'OP_Pac_ANZ', '2015-08-11 02:54:00.000'),
('1508130106', 'OP_Pac_ANZ', '2015-08-13 14:26:00.000'),
('1509300036', 'OP_Pac_ANZ', '2015-09-30 05:08:00.000'),
('1508190197', 'OP_Pac_ANZ', '2015-08-19 20:24:00.000'),
('1509230005', 'OP_Pac_ANZ', '2015-09-23 01:18:00.000'),
('1509230194', 'OP_Pac_ANZ', '2015-09-23 21:41:00.000'),
('1511230002', 'OP_Pac_ANZ', '2015-11-23 04:13:00.000'),
('1511240121', 'OP_Pac_ANZ', '2015-11-24 21:05:00.000'),
('1601240046', 'OP_Pac_ANZ', '2016-01-24 21:41:00.000'),
('1603200028', 'OP_Pac_ANZ', '2016-03-20 22:46:00.000'),
('1602010003', 'OP_Pac_ANZ', '2016-02-01 03:35:00.000'),
('1608090001', 'OP_Pac_ANZ', '2016-08-09 00:49:00.000');

2 个答案:

答案 0 :(得分:0)

问题可能是PIVOT条款显示为FOR [Period] IN ([1], [2], ...),而实际上并非[Period]的值。

FOR [Period] IN (['20151'], ['20152'], ...)

(或类似的东西,我不是一个测试确切语法的好地方)应该工作。如果您不想专门编码年份/ qtr值,则可以直接使用Period而不是Period的排名函数(但这仍然会在某些方面做出一些假设)要显示的值的数量......)

顺便说一句,你有"权利" LEFT JOIN子句中WHERE的表格,会破坏外部联接。您应该将该条件移至连接的ON子句。

答案 1 :(得分:0)

我想这段关系可以重新检查?如果从样本数据中检索NULL值,那么COALESCE肯定没有从第一列中找到数据,这可能会解释数据字段中的NULL。