Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey all,

I'm trying to carry out a pivot on a table in a SQL Server Database but am hitting an unhelpful error message. My query is as follows:

SQL
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)
SELECT @ColumnName= ISNULL(@ColumnName + ', ','') + QUOTENAME(EnquiryPeriod)
FROM (SELECT DISTINCT EnquiryPeriod FROM Enquiries) AS EnqPeriod

SET @DynamicPivotQuery =
  N'SELECT Children.ChildID, ' + @ColumnName + '
    FROM (Enquiries
    INNER JOIN CarerChildren ON CarerChildren.CarerID = Enquiries.CarerID)
    INNER JOIN Children ON CarerChildren.ChildID = Children.ChildID
    WHERE (((CarerChildren.DateMovedOn) Is Null))
    GROUP BY Children.ChildID, [Firstname] + [Surname], Children.DateOfBirth
    ORDER BY [Firstname] + [Surname], Enquiries.EnquiryPeriod
    PIVOT(Max(EnquiryDate)
          FOR EnquiryPeriod IN (' + @ColumnName + ')) AS PVTTable'

EXEC (@DynamicPivotQuery)


It gives the error "Incorrect syntax near the word 'PIVOT'. It's probably a simple error, just wondering if anyone can spot what I'm doing wrong?
Posted
Updated 21-Aug-14 15:08pm
v2

I havn't worked using pivot so its just a guess. Don't mind if I'm mistaken.
I think you need to follow this syntax as below
XML
SELECT <non-pivoted column>,
    [first pivoted column] AS <column name>,
    [second pivoted column] AS <column name>,
    ...
    [last pivoted column] AS <column name>
FROM
    (<SELECT query that produces the data>)
    AS <alias for the source query>
PIVOT
(
    <aggregation function>(<column being aggregated>)
FOR
[<column that contains the values that will become column headers>]
    IN ( [first pivoted column], [second pivoted column],
    ... [last pivoted column])
) AS <alias for the pivot table>
<optional ORDER BY clause>;


so I think you need to put order by after pivot?

Reference: Using PIVOT and UNPIVOT-TechNet-Microsoft[^]
 
Share this answer
 
You need to modify the sub-query that you use for your source so that it has all columns that will be used for your PIVOT, like this:

SQL
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)
SELECT @ColumnName= ISNULL(@ColumnName + ', ','') + QUOTENAME(EnquiryPeriod)
FROM (SELECT DISTINCT EnquiryPeriod FROM Enquiries) AS EnqPeriod
 
SET @DynamicPivotQuery =
  N'SELECT ChildID, [childName], ' + @ColumnName + '
    FROM (SELECT Children.ChildID, [Firstname] + [Surname] as [childName],
    Enquiries.EnquiryPeriod, EnquiryDate
    FROM Enquiries
    INNER JOIN CarerChildren ON CarerChildren.CarerID = Enquiries.CarerID)
    INNER JOIN Children ON CarerChildren.ChildID = Children.ChildID
    WHERE CarerChildren.DateMovedOn Is Null) AS pvtSource
    PIVOT(Max(EnquiryDate)
          FOR EnquiryPeriod IN (' + @ColumnName + ')) AS PVTTable'
 
EXEC (@DynamicPivotQuery)
 
Share this answer
 
Two things:
-> ISNULL(@ColumnName + ', ','') better: COALESCE(@ColumnName +', ', '')
-> is QUOTENAME(EnquiryPeriod) separating also with a comma? NO but if the distinct results in more than 1 row.....you have a problem.
In my article [^]you see a method to deal with that
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900