Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Unpivot not working

Posted on 2014-12-18
3
Medium Priority
?
102 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
[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
3 Comments
 
LVL 70

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

636 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