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 2005 Forums
 Transact-SQL (2005)
 SQL output format issues

Author  Topic 

sridhar3004
Starting Member

34 Posts

Posted - 2012-07-27 : 07:47:55
I've the following query

Name Group Expense Budget Variation
Sridhar Conveyance 1500 2000 500
Travel Allownce 100 200 100
Rajesh Conveyance 700 2000 1300
Travel Allownce 50 200 150

I want the final output to be as follows

Name ConveyanceExpense ConveyanceBudget ConveyanceVariation TravelAllownceExpense TravelAllownceBudget TravelAllownceVariation
Sridhar 1500 2000 500 100 200 100
Rajesh 700 2000 1300 50 200 150

ANy help is appreciated

Thanks


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-27 : 10:30:22
[code]
SELECT Name,
SUM(CASE WHEN Group = 'Conveyance' THEN Expense ELSE 0 END) AS ConveyanceExpense,
SUM(CASE WHEN Group = 'Conveyance' THEN Budget ELSE 0 END) AS ConveyanceBudget,
SUM(CASE WHEN Group = 'Conveyance' THEN Variation ELSE 0 END) AS ConveyanceVariation,
SUM(CASE WHEN Group = 'Travel Allownce' THEN Expense ELSE 0 END) AS TravelAllownceExpense,
SUM(CASE WHEN Group = 'Travel Allownce' THEN Budget ELSE 0 END) AS TravelAllownceBudget,
SUM(CASE WHEN Group = 'Travel Allownce' THEN Variation ELSE 0 END) AS TravelAllownceVariation
FROM tablename
GROUP BY Name
[/code]

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

Go to Top of Page
   

- Advertisement -