Solved

SQL Unpivot not working

Posted on 2014-12-18
3
95 Views
Last Modified: 2014-12-23
I am trying to get my sql query to work and it is not. It is telling me incorrect syntax near 'UNPIVOT'

i am using sql 2005(i know its not used but 3rd party software uses it)

WITH TESTTABLE AS
(
    SELECT    * 
    FROM      (
                  SELECT    xrefid,appdate, closedate, log_MS_DATE_HUDAPPROVAL
                      
                  FROM      emdb.dbo.calendarview
              )p
    UNPIVOT 
    ( 
        Result FOR eventdate  in (appdate, closedate,log_MS_DATE_HUDAPPROVAL)
    )unpvt
)

SELECT    T.xrefid,
          M.eventdate,
          M.Result,
          T.Total
FROM      Table1 T
          JOIN TESTTABKE M
              ON T.xrefid = M.xrefid

Open in new window

0
Comment
Question by:desiredforsome
  • 2
3 Comments
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40507899
Make sure the db compatibility level is not set to SQL 2000, which I think is 80.  That would cause "UNPIVOT" to be unrecognized syntax.

If it is, I think you can get around the issue by running the SQL from a db set to level 90, such as master or tempdb.
0
 

Accepted Solution

by:
desiredforsome earned 0 total points
ID: 40507940
I figured it out.
;WITH MyCTE AS
(
    SELECT    * 
    FROM      (
                  SELECT   *
                  FROM      emdb.dbo.calendarview 
              )p
    UNPIVOT 
    ( 
        EventDate FOR DateDescription in ([Appraisal Ordered],[Closing Date],[Application Signed],[Appraisal Recieved] ,[Approval To Close],[Insurance Ordered],[Title Ordered])
    ) as unpvt
)

insert into outlookreport.dbo.calendar (eventdate,item,xrefid)
SELECT    
          M.EventDate,
          M.DateDescription,
         
          T.xrefid
FROM      emdb.dbo.calendarview T 

          JOIN MyCTE M
              ON T.xrefid = M.xrefid

Open in new window

0
 

Author Closing Comment

by:desiredforsome
ID: 40514590
yup
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
execute a MS SQL script as a schedule SQL job 72 139
Not selecting duplicate data 6 60
Impove long SQL Stored Procedure Performance 14 78
CROSS APPLY 4 57
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…

830 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