Solved

SQL Unpivot not working

Posted on 2014-12-18
3
91 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

24 Experts available now in Live!

Get 1:1 Help Now