SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQl query presented data in pivot way - edited
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

w1102157
Yak Posting Veteran

80 Posts

Posted - 07/16/2012 :  08:34:43  Show Profile  Reply with Quote
Hi guys

kind of a similar question to what i asked before, i have been assked to present data in a certain way,

i have extracted the data as below


Pr cost Type key G
CW 244 EL 1 Dj
CW 22 NL 2 Dj
IM 24 EL 3 Dj
IM 140 NL 4 Dj
CW 100 EL 5 DRk
CW 23 NL 6 DRk
IM 45 EL 7 DRk
IM 40 NL 8 DRk
CW 46 EL 9 DRS
CW 23 NL 10 DRS
IM 23 EL 11 DRS
IM 12 NL 12 DRS
CW 10 EL 13 DZA
CW 0 NL 14 DZA
IM 0 EL 15 DZA
IM 0 NL 16 DZA

however the manager wants me to present this as below

G key [CW EL] [CW NL] [Im El] [im NL]
Dj 1 244 22 25 40
DRk 2 100 23 45 40
DRS 3 46 23 23 12
DZA 4 10 0 0 0

can anyone help me scrip this?

thank you






Edited by - w1102157 on 07/16/2012 09:37:23

visakh16
Very Important crosS Applying yaK Herder

India
47040 Posts

Posted - 07/16/2012 :  09:43:30  Show Profile  Reply with Quote

SELECT G,
ROW_NUMBER() OVER (ORDER BY MIN([Key])) AS [Key],
SUM(CASE WHEN Pr='CW' AND Type='EL' THEN cost ELSE 0 END) AS [CW EL],
SUM(CASE WHEN Pr='CW' AND Type='NL' THEN cost ELSE 0 END) AS [CW NL],
SUM(CASE WHEN Pr='CW' AND Type='EL' THEN cost ELSE 0 END) AS [im EL],
SUM(CASE WHEN Pr='CW' AND Type='NL' THEN cost ELSE 0 END) AS [im NL]
FROM table
GROUP BY G


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/


Edited by - visakh16 on 07/16/2012 09:44:19
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000