Solved

Unpivot Select?

Posted on 2010-08-30
10
281 Views
Last Modified: 2012-06-27
My attached code works fine and returns my data like this:

AmtRequested     FunderAmtNet     AnnuitantAmtAdj     Profit
613743.05             593867.82      340500.00          253367.8200

I need it unpivoted?  So I see the data this way...

FundsType                Amt
AmtRequested        613743.05
FunderAmtNet         593867.82
AnnuitantAmtAdj    340500.00    
Profit                       253367.8200

SELECT	Sum(u.funderAmtRequested) AS AmtRequested,
		Sum(u.FunderAmtNet) as FunderAmtNet, 
		Sum(u.AnnuitantAmtAdj) as AnnuitantAmtAdj, 
		Sum(u.Profit) as Profit 
			
FROM	CRMPROD_01.dbo.vP_UnionPWDealsEventsSub u INNER JOIN 
		CRMPROD_01.dbo.P_Funders f ON u.FunderID = f.FunderID INNER JOIN
		CRMPROD_01.dbo.leads l ON u.LeadID = l.id
WHERE	(NOT (l.last_name LIKE 'Test%'))

Open in new window

0
Comment
Question by:lrbrister
[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
  • 5
  • 3
  • 2
10 Comments
 
LVL 2

Expert Comment

by:rajeshprasath
ID: 33559743
SELECT FundsType, Amt
FROM
(
SELECT      Sum(u.funderAmtRequested) AS AmtRequested,
            Sum(u.FunderAmtNet) as FunderAmtNet,
            Sum(u.AnnuitantAmtAdj) as AnnuitantAmtAdj,
            Sum(u.Profit) as Profit
                  
FROM      CRMPROD_01.dbo.vP_UnionPWDealsEventsSub u INNER JOIN
            CRMPROD_01.dbo.P_Funders f ON u.FunderID = f.FunderID INNER JOIN
            CRMPROD_01.dbo.leads l ON u.LeadID = l.id
WHERE      (NOT (l.last_name LIKE 'Test%'))
) p
UNPIVOT
(
FundsType IN (AmtRequested, FunderAmtNet, AnnuitantAmtAdj, Profit )
) AS UNPVT
0
 

Author Comment

by:lrbrister
ID: 33559805
rajeshprasath:
Getting an error on
 
Msg 156, Level 15, State 1, Line 16
Incorrect syntax near the keyword 'IN'.
...
UNPIVOT
(
FundsType IN (AmtRequested, FunderAmtNet, AnnuitantAmtAdj, Profit )
) AS UNPVT
0
 
LVL 41

Assisted Solution

by:ralmada
ralmada earned 100 total points
ID: 33559830
correct syntax should be:
SELECT FundsType, Amt
FROM
(
SELECT      Sum(u.funderAmtRequested) AS AmtRequested,
            Sum(u.FunderAmtNet) as FunderAmtNet, 
            Sum(u.AnnuitantAmtAdj) as AnnuitantAmtAdj, 
            Sum(u.Profit) as Profit 
                  
FROM      CRMPROD_01.dbo.vP_UnionPWDealsEventsSub u INNER JOIN 
            CRMPROD_01.dbo.P_Funders f ON u.FunderID = f.FunderID INNER JOIN
            CRMPROD_01.dbo.leads l ON u.LeadID = l.id
WHERE      (NOT (l.last_name LIKE 'Test%'))
) p
UNPIVOT
(
Amt for FundsType IN (AmtRequested, FunderAmtNet, AnnuitantAmtAdj, Profit )
) AS UNPVT

Open in new window

0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 2

Accepted Solution

by:
rajeshprasath earned 400 total points
ID: 33559866
sorry, try this, it will work.....

SELECT FundsType, Amt
FROM
(
SELECT      Sum(u.funderAmtRequested) AS AmtRequested,
            Sum(u.FunderAmtNet) as FunderAmtNet,
            Sum(u.AnnuitantAmtAdj) as AnnuitantAmtAdj,
            Sum(u.Profit) as Profit
                 
FROM      CRMPROD_01.dbo.vP_UnionPWDealsEventsSub u INNER JOIN
            CRMPROD_01.dbo.P_Funders f ON u.FunderID = f.FunderID INNER JOIN
            CRMPROD_01.dbo.leads l ON u.LeadID = l.id
WHERE      (NOT (l.last_name LIKE 'Test%'))
) p
UNPIVOT
(
Amt for FundsType IN (AmtRequested, FunderAmtNet, AnnuitantAmtAdj, Profit )
) AS UNPVT
0
 

Author Comment

by:lrbrister
ID: 33559870
ralmada:
Perfect
0
 
LVL 2

Expert Comment

by:rajeshprasath
ID: 33559916
Msg 8167, Level 16, State 1, Line 1
The type of column "Profit" conflicts with the type of other columns specified in the UNPIVOT list.

if you still get the above error,

just convert the profit column to numeric with scale 2.
0
 

Author Comment

by:lrbrister
ID: 33560072
rajeshprasath:
My apologies...
I should have given you a chance to correct it before I accpted a different answer.
0
 

Author Comment

by:lrbrister
ID: 33560261
Allright...
Now I'm in a pickle...
I thought I accepted ralmada: answer...which was correct.
Just a few minutes later rajeshprasath: posted his corrected answer...which I thought was too late.
You guys mind splitting the points and someone just yell at me?
0
 
LVL 41

Expert Comment

by:ralmada
ID: 33560380
I would suggest:
400 points rajeshprashath
100 points ralmada
 
0
 

Author Comment

by:lrbrister
ID: 33560405
I'm cool with that. I'll post that shortly
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

752 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