2013-05-10 89 views
0

为什么,为什么?我是否收到错误:数据透视表错误

“Msg 325,Level 15,State 1,Line 17 'PIVOT'附近的语法不正确您可能需要将当前数据库的兼容性级别设置为更高的值以启用此功能。请参阅存储过程sp_dbcmptlevel的帮助。“ 对于此查询?

WITH Offnet7 AS (
    SELECT disposition.dispositiondesc, interaction.dispositionid, DATEPART(wk,interaction.ibegintime) as iWeek 

    FROM interaction INNER JOIN 
      disposition ON interaction.reasonid = disposition.dispositionid 

    WHERE interaction.dispositionid = 10 and (reasonid = 20365 or reasonid = 20366 or reasonid = 11168) and 
      interaction.ibegintime >= '2013-1-1' and 
      interaction.ibegintime < '2014-1-1' 
) 

SELECT iWeek, dispositiondesc, count(iWeek) as 'OffnetCounts' 
FROM Offnet7 

Group by dispositiondesc, iWeek 

PIVOT 
(
    OffnetCounts 
    for iWeek in ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15]) 
) AS counts 

编辑:“构建模式” 试图使SQL小提琴,当我去给它哽咽(SQL Noob) 我从SQL管理工作室的对象浏览器中提取类型并复制了一些示例数据。但是,这是我尝试:

CREATE TABLE interaction 
    ([reasonid] int, [dispositionid] int, [ibegintime] datetime) 
; 

INSERT INTO interaction 
    ([reasonid], [dispositionid], [ibegintime]) 
VALUES 
    (20366, 10, '2012-01-31 23:59:48.000'), 
    (20366, 10, '2012-02-07 14:03:01.000'), 
    (20366, 10, '2012-02-07 14:06:48.000'), 
    (20366, 10, '2012-02-13 21:44:10.000'), 
    (20366, 10, '2012-02-27 21:36:33.000') 
; 


CREATE TABLE disposition 
    ([dispositionid] int, [predefined] int, [dispositiondesc] varchar(64), [displayvalue] varchar(254)) 
; 

INSERT INTO disposition 
    ([dispositionid], [predefined], [dispositiondesc], [displayvalue]) 
VALUES 
(10, 1, 'TRANSFERRED OFFNET', 'TRANSFERRED OFFNET'), 
    (11168, 0, 'TAKEDA PASSWORD', 'TAKEDA PASSWORD'), 
    (15433, 0, 'Voice Mail - TAKEDAEMEA', 'Voice Mail - TAKEDAEMEA'), 
    (20365, 0, 'TAKEDA iPAD, iPhone or BlackBerry', 'TAKEDA iPAD, iPhone or BlackBerry'), 
    (20366, 0, 'TAKEDA Concur', 'TAKEDA Concur') 
; 

结论: 感谢所有帮助Bluefeet!

对于任何对此感兴趣的人,如果我的DBA正确设置了SQL兼容性级别,他的第一个答案会起作用。想他的第一个答案之后,我得到了一个:

"Msg 102, Level 15, State 1, Line 19 Incorrect syntax near '('." 

因为DBA没有与支持PIVOT语句中的兼容性级别配置的SQL Server。

回答

2

您的语法关闭。 PIVOT正在执行GROUP BY和聚合。在我看来,要使用:

WITH Offnet7 AS 
(
    SELECT disposition.dispositiondesc, 
    interaction.dispositionid, 
    DATEPART(wk,interaction.ibegintime) as iWeek 
    FROM interaction 
    INNER JOIN disposition 
    ON interaction.reasonid = disposition.dispositionid 
    WHERE interaction.dispositionid = 10 
    and (reasonid = 20365 or reasonid = 20366 or reasonid = 11168) 
    and interaction.ibegintime >= '2013-1-1' 
    and interaction.ibegintime < '2014-1-1' 
) 
SELECT dispositiondesc, 
    [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15] 
FROM Offnet7 
PIVOT 
(
    count(dispositionid) 
    for iWeek in ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15]) 
) AS counts; 

Demo

这将创建数据表与dispositionid的的每个星期的计数,由dispositiondesc分组。

编辑,这也可以使用聚合函数与CASE表达做到:

SELECT disposition.dispositiondesc, 
    sum(case when DATEPART(wk,interaction.ibegintime) = 1 then 1 else 0 end) [1], 
    sum(case when DATEPART(wk,interaction.ibegintime) = 2 then 1 else 0 end) [2], 
    sum(case when DATEPART(wk,interaction.ibegintime) = 3 then 1 else 0 end) [3], 
    sum(case when DATEPART(wk,interaction.ibegintime) = 4 then 1 else 0 end) [4], 
    sum(case when DATEPART(wk,interaction.ibegintime) = 5 then 1 else 0 end) [5], 
    sum(case when DATEPART(wk,interaction.ibegintime) = 6 then 1 else 0 end) [6], 
    sum(case when DATEPART(wk,interaction.ibegintime) = 7 then 1 else 0 end) [7], 
    sum(case when DATEPART(wk,interaction.ibegintime) = 8 then 1 else 0 end) [8], 
    sum(case when DATEPART(wk,interaction.ibegintime) = 9 then 1 else 0 end) [9], 
    sum(case when DATEPART(wk,interaction.ibegintime) = 10 then 1 else 0 end) [10], 
    sum(case when DATEPART(wk,interaction.ibegintime) = 11 then 1 else 0 end) [11], 
    sum(case when DATEPART(wk,interaction.ibegintime) = 12 then 1 else 0 end) [12], 
    sum(case when DATEPART(wk,interaction.ibegintime) = 13 then 1 else 0 end) [13], 
    sum(case when DATEPART(wk,interaction.ibegintime) = 14 then 1 else 0 end) [14], 
    sum(case when DATEPART(wk,interaction.ibegintime) = 15 then 1 else 0 end) [15] 
FROM interaction 
INNER JOIN disposition 
    ON interaction.reasonid = disposition.dispositionid 
WHERE interaction.dispositionid = 10 
    and (reasonid = 20365 or reasonid = 20366 or reasonid = 11168) 
    and interaction.ibegintime >= '2013-1-1' 
    and interaction.ibegintime < '2014-1-1' 
group by disposition.dispositiondesc; 

SQL Fiddle with Demo

+0

我仍然得到一个“消息102,级别15,状态1,第19行不正确('。')在这个错误? 据我可以告诉它看起来好吗?(我是一个SQL noob,但是。) – NoMoreZealots 2013-05-14 20:39:15

+0

@NoMoreZealots你可以创建一个sql小提琴与一些示例数据? – Taryn 2013-05-14 20:40:55

+0

如果它是语法错误是会帮助吗? – NoMoreZealots 2013-05-14 20:55:58