Solved

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

Posted on 2012-04-12
2
265 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 500 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

740 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