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)
 Display result rows in a column

Author  Topic 

R.Prabu
Starting Member

33 Posts

Posted - 2008-08-29 : 11:33:24
Hi This my quesry

Select MainReq_Reason, SUM(ISNULL(Asset, 0)) As Cost
From HSSPMS_TblMaintenanceCostDetails Group By MainReq_Reason

My Result is this

MainReq_Reason Cost
---------------------- -------
Assesories 5000.00
Electrics 9000.00
Heating 100.00
Labour 2500.00
Others 1000.00


I need to display

AssetCost Assesories Electrics Heating Labour Others

Cost 5000.00 9000.00 100.00 2500.00 1000.00


Any one help me


Regards,
Prabu R

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2008-08-29 : 12:07:10
Try this

Declare @tbl table(MainReq_Reason varchar(25),Cost money)
Insert Into @TBl
select 'Assesories', 5000.00 union all
select 'Electrics' ,9000.00 union all
select 'Heating' ,100.00 union all
select 'Labour' ,2500.00 union all
select 'Others' ,1000.00

Select 'COST' as AssetCost,*
from @TBl a
Pivot (Sum(Cost) for MainReq_Reason in (Assesories,Electrics,Heating,Labour,Others)) p

/*
AssetCost Assesories Electrics Heating Labour Others
------ --------------------- --------------------- --------------------- --------------------- ---------------------
COST 5000.00 9000.00 100.00 2500.00 1000.00
*/
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-09-01 : 03:48:34
If MainReq_Reason column values are dynamic, use
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -