Solved

SQL Unpivot not working

Posted on 2014-12-18
3
89 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:ScottPletcher
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now