1

When I run a simple query, where AskiaID is a numeric field, in MS SQL Server Management Studio, I get this :-

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 '('.

If I change the query to its original form :-

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'.

Any ideas what I'm doing wrong?

3
  • Use a subquery to fix the column. Commented Feb 21, 2020 at 15:11
  • Care to expand on your idea please Gordon? Commented Feb 21, 2020 at 15:20
  • 1
    One thing you are doing wrong is casting to char but not specifying the length. This is very problematic...but in this case you got lucky. This post discusses why it is bad with varchar but the same logic applies to char. sqlblog.org/2009/10/09/… Of course, since you have varying lengths of data you should probably be using varchar anyway. Commented Feb 21, 2020 at 15:36

2 Answers 2

3
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;
Sign up to request clarification or add additional context in comments.

3 Comments

<pre>DECLARE @Columns as VARCHAR(MAX) DECLARE @SQL as VARCHAR(MAX) SELECT @Columns=COALESCE(@Columns+',','')+QUOTENAME(AgentID) FROM (SELECT DISTINCT AgentID FROM [RSM.Statistics].[dbo].[Statistic_Agent] where TaskID=1937) AS B ORDER BY AgentID SET @SQL='SELECT TaskResult,'+@Columns+' FROM (SELECT TaskResult,AgentID,1 AS rownum FROM [RSM.Statistics].[dbo].[Statistic_Agent] where TaskID=1937) as PivotData PIVOT (COUNT(rownum) FOR AgentID IN ('+@Columns+')) AS PivotResult' PRINT @SQL EXEC(@SQL)</pre>
nice!!! there is no actual need for a derived rownum column, you could use a non nullable column eg. TaskID .... ' FROM (SELECT TaskResult,AgentID, Taskid FROM [RSM.Statistics].[dbo].[Statistic_Agent] where TaskID=1937) as PivotData PIVOT (COUNT(Taskid) FOR AgentID....
Yes, you could avoid the use of the rownum column. I may develop this code further, for example, to perform more advanced calculations than just counting. Sums of costs or hours, say, so the rownum column is useful. Thanks for your help btw :)
0

Have you tried using strings but not casting?

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

1 Comment

the query will raise error: Invalid column name 'TaskResult'. because TaskResult is in the aggregation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.