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)
 How to select data from same coumn into diff rows

Author  Topic 

rohan_iiitc
Starting Member

11 Posts

Posted - 2010-05-05 : 08:13:28
Dear Friends

I would like to know how to get the below output.


Table Structure is of TBL_BRANCH :
Branch, BranchName, TYPE, SALE_QTY

Desired Output
-------------------------------------------
| BRANCH | TYPE1 | TYPE2 | TYPE3 | TOTAL |
-------------------------------------------
| BRANCH1 | 05 | 12 | 09 | 26 |
-------------------------------------------
| BRANCH2 | 01 | 04 | 19 | 24 |
-------------------------------------------
| BRANCH3 | 15 | 22 | 01 | 38 |
-------------------------------------------


Regards
Rohan

apodemus
Starting Member

30 Posts

Posted - 2010-05-05 : 08:46:37
IF list of Type's is constant and known, you can use PIVOT :

select BranchName Branch,[TYPE1], [TYPE2], [TYPE3], [TOTAL]
from
(select BranchName, type, sale_qty
from TBL_BRANCH
union all
select BranchName, 'TOTAL' type, sale_qty
from TBL_BRANCH

) pp
pivot
(
SUM(sale_qty)
FOR type IN ([TYPE1], [TYPE2], [TYPE3], [TOTAL])
) as pt

i don't have you table, sorry for sythax's if will be somone :)

apodemus
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-05-05 : 14:57:48
you can just add up individual ones to get total no need of union all query

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

Go to Top of Page
   

- Advertisement -