?
Solved

SYNTAX ERROR WITH IN CLAUSE IN PIVOT QUERY Msg 102, Level 15, State 1, Line 24

Posted on 2012-04-12
2
Medium Priority
?
272 Views
Last Modified: 2012-06-21
declare @MyCol as varchar(250)

 set @MyCol = (SELECT     R2.MYMONTH + ', '
                            FROM          SALES_TABLE_Col R2
                             FOR XML PATH(''))

SELECT
SALE_LINE, SALES_PART, SALES_PART_DESC, 'MYSORT' AS SORT_TYPE, OVERDUE, ' + @MyCol + ' FUTUREDUE, PENDING
FROM
( SELECT SALE_LINE, SALES_PART, SALES_PART_DESC, MYMONTH, SUM(SALES_AMT) AS SALES_AMT
FROM
(SELECT SALE_LINE, SALES_PART, SALES_PART_DESC,
CASE WHEN SALES_DATE < GETDATE() AND STATUS <> 'H' THEN 'OVERDUE'
       WHEN SALES_DATE > [ENDING_DATE] AND STATUS <> 'H' THEN 'FUTUREDUE'
       WHEN STATUS = 'H' OR SALES_DATE IS NULL THEN 'PENDING'
       ELSE MYMONTH END AS MYMONTH,
 SALES_AMT
FROM SALES_TABLE
) MYSALES
GROUP BY SALE_LINE, SALES_PART, SALES_PART_DESC, MYMONTH
) AS SourceTable
PIVOT
(SUM(SALES_AMT)
    FOR [MYMONTH] IN ( OVERDUE,  ' + @MyCol + '  FUTUREDUE, PENDING)
     ) AS PivotTable
0
Comment
Question by:NettieEckenrode
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 2000 total points
ID: 37840308
declare @MyCol as varchar(250)

 set @MyCol = (SELECT R2.MYMONTH + ', ' FROM SALES_TABLE_Col R2 FOR XML PATH(''))


declare @sql varchar(8000) = '
SELECT
SALE_LINE, SALES_PART, SALES_PART_DESC, ''MYSORT'' AS SORT_TYPE, OVERDUE, ' + @MyCol + ' FUTUREDUE, PENDING
FROM
( SELECT SALE_LINE, SALES_PART, SALES_PART_DESC, MYMONTH, SUM(SALES_AMT) AS SALES_AMT
FROM
(SELECT SALE_LINE, SALES_PART, SALES_PART_DESC,
CASE WHEN SALES_DATE < GETDATE() AND STATUS <> ''H'' THEN ''OVERDUE''
       WHEN SALES_DATE > [ENDING_DATE] AND STATUS <> ''H'' THEN ''FUTUREDUE''
       WHEN STATUS = ''H'' OR SALES_DATE IS NULL THEN ''PENDING''
       ELSE MYMONTH END AS MYMONTH,
 SALES_AMT
FROM SALES_TABLE
) MYSALES
GROUP BY SALE_LINE, SALES_PART, SALES_PART_DESC, MYMONTH
) AS SourceTable
PIVOT
(SUM(SALES_AMT)
    FOR [MYMONTH] IN ( OVERDUE,  ' + @MyCol + ' FUTUREDUE, PENDING)
     ) AS PivotTable
'    

-- print @sql

exec(@sql)
0
 

Author Closing Comment

by:NettieEckenrode
ID: 37840460
I had to change the following:
 
 declare @sql varchar(8000) = '
SELECT

to

  declare @sql varchar(8000)

set @sql = '  select

Thanks so much.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question