SQL Серверная сводная таблица с нумерацией c Столбцы - PullRequest
0 голосов
/ 21 февраля 2020

Когда я запускаю простой запрос, где AskiaID представляет собой поле числового c, в MS SQL Server Management Studio я получаю следующее: -

SELECT TaskResult, [80444], [80421], [80438], [80435], [80046], [80427], [80378], [80442], [80419], [80436], [80379], [80410], [80439], [80437], [80405], [80377], [80383], [80406], [80420], [80443] FROM [RSM.Statistics].[dbo].[Statistic_Call] as PivotData
PIVOT
(
   COUNT(TaskResult)
   FOR RTRIM(CAST(AgentID AS CHAR)) IN ([80444], [80421], [80438], [80435], [80046], [80427], [80378], [80442], [80419], [80436], [80379], [80410], [80439], [80437], [80405], [80377], [80383], [80406], [80420], [80443])
) AS PivotResult
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near '('.

Если я изменяю запрос на его оригинальная форма: -

SELECT TaskResult, [80444], [80421], [80438], [80435], [80046], [80427], [80378], [80442], [80419], [80436], [80379], [80410], [80439], [80437], [80405], [80377], [80383], [80406], [80420], [80443] FROM [RSM.Statistics].[dbo].[Statistic_Call] as PivotData
PIVOT
(
   COUNT(TaskResult)
   FOR AgentID IN (80444, 80421, 80438, 80435, 80046, 80427, 80378, 80442, 80419, 80436, 80379, 80410, 80439, 80437, 80405, 80377, 80383, 80406, 80420, 80443)
) AS PivotResult
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near '80444'.

Есть идеи, что я делаю не так?

Ответы [ 2 ]

1 голос
/ 21 февраля 2020
declare @Statistic_Call table
(
    AgentId int,
    TaskResult varchar(10)
)
insert into @Statistic_Call(AgentId, TaskResult)
values
(80444, 'Test 1'),
(80444, 'Test 2'),
(80421, 'Test 1'),
(80438, 'Test 2'),
(80435, 'Test 1'),
(80046, 'Test 1'),
(80427, 'Test 3'),
(80378, 'Test 1'),
(80442, 'Test 1'),
(80419, 'Test 4'),
(80436, 'Test 1'),
(80379, 'Test 2'),
(80410, 'Test 1'),
(80439, 'Test 1'),
(80437, 'Test 3'),
(80405, 'Test 1'),
(80377, 'Test 2'),
(80383, 'Test 1'),
(80406, 'Test 1'),
(80420, 'Test 3'),
(80443 ,'Test 1');


SELECT TaskResult, [80444], [80421], [80438], [80435], [80046], [80427], [80378], [80442], [80419], [80436], [80379], [80410], [80439], [80437], [80405], [80377], [80383], [80406], [80420], [80443] 
FROM 
(
select TaskResult, AgentId, 1 as rownum --row_number() over (order by(select null)) as rownum
from @Statistic_Call as PivotData
) as src
PIVOT
(
   count(rownum)
   FOR AgentId IN ([80444], [80421], [80438], [80435], [80046], [80427], [80378], [80442], [80419], [80436], [80379], [80410], [80439], [80437], [80405], [80377], [80383], [80406], [80420], [80443])
) AS PivotResult;
0 голосов
/ 21 февраля 2020

Вы пытались использовать строки, но не приводили?

SELECT TaskResult, [80444], [80421], [80438], [80435], [80046], [80427], [80378], [80442], [80419], [80436], [80379], [80410], [80439], [80437], [80405], [80377], [80383], [80406], [80420], [80443] FROM [RSM.Statistics].[dbo].[Statistic_Call] as PivotData
PIVOT
(
   COUNT(TaskResult)
   FOR AgentID IN ([80444], [80421], [80438], [80435], [80046], [80427], [80378], [80442], [80419], [80436], [80379], [80410], [80439], [80437], [80405], [80377], [80383], [80406], [80420], [80443])
) AS PivotResult
...