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。
我仍然得到一個「消息102,級別15,狀態1,第19行不正確('。')在這個錯誤? 據我可以告訴它看起來好嗎?(我是一個SQL noob,但是。) – NoMoreZealots 2013-05-14 20:39:15
@NoMoreZealots你可以創建一個sql小提琴與一些示例數據? – Taryn 2013-05-14 20:40:55
如果它是語法錯誤是會幫助嗎? – NoMoreZealots 2013-05-14 20:55:58