Solved

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

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

735 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