Solved

SQL Unpivot not working

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 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