Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 SQL Help with rowover

Author  Topic 

Ratz03
Starting Member

27 Posts

Posted - 2014-12-29 : 15:25:26
Hi Experts,

I am trying to accomplish the below using SQL and require help. I am trying to calculate some financials using one query. Source Table 1. Output should be like table 2
col a col b col c
P1 C 2
P1 I 3
P1 P 1
P1 C 5
P1 I 1
P1 P 3
P2 C 1
P2 I 2
P2 P 3
P2 C 4
P2 I 5
P2 P 6


Table 2
Col a Col b col c col d
p1 sum of all P's sum of all I's sum of all p's
P2 sum of all P's sum of all I's sum of all p's


Please advise. many thanks

ScottPletcher
Aged Yak Warrior

550 Posts

Posted - 2014-12-29 : 16:05:31
Something like this:

SELECT [Col a] AS col_a,
SUM(CASE WHEN [Col b] = 'C' THEN [col c] ELSE 0 END) AS Col_b,
SUM(CASE WHEN [Col b] = 'I' THEN [col c] ELSE 0 END) AS Col_c,
SUM(CASE WHEN [Col b] = 'P' THEN [col c] ELSE 0 END) AS Col_d
FROM table_name
GROUP BY [Col a]

Go to Top of Page

Ratz03
Starting Member

27 Posts

Posted - 2014-12-30 : 16:22:49
thankyou scottPletcher
Go to Top of Page
   

- Advertisement -