Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
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 cP1 C 2P1 I 3P1 P 1P1 C 5P1 I 1P1 P 3P2 C 1P2 I 2P2 P 3P2 C 4P2 I 5P2 P 6Table 2Col a Col b col c col dp1 sum of all P's sum of all I's sum of all p'sP2 sum of all P's sum of all I's sum of all p'sPlease 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_dFROM table_nameGROUP BY [Col a]