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)
 how to get this output?

Author  Topic 

shaik.zakeer
Posting Yak Master

117 Posts

Posted - 2008-04-25 : 06:29:15


i hav a table lik this

bank amount status
----------------------------
hdfc 1000 credit
icici 2000 credit
hdfc 500 debit
icici 1000 debit

i need to get the output like diz


bank credit debit
--------------------------------
hdfc 1000 500
icici 2000 1000

how can i get this output????

Thanks

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2008-04-25 : 06:37:41
Look up Pivot in book online. Should do what you want.

--
Gail Shaw
Go to Top of Page

dass05555
Yak Posting Veteran

55 Posts

Posted - 2008-04-25 : 06:57:27
select bank,
[credit] as credit,
[debit] as debit
from (select bank,amount,status from bank)as source
pivot
(
sum(amount )
for status in ([credit],[debit])
)as pivot_table


chk it out my dear friend...,



cool...,
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-25 : 07:03:34
or use group by

SELECT bank,
SUM(CASE WHEN status='credit' THEN amount ELSE 0 END) AS credit,
SUM(CASE WHEN status='debit' THEN amount ELSE 0 END) AS debit
FROM Table
GROUP BY bank
Go to Top of Page

shaik.zakeer
Posting Yak Master

117 Posts

Posted - 2008-04-25 : 07:17:30
quote:
Originally posted by visakh16

or use group by

SELECT bank,
SUM(CASE WHEN status='credit' THEN amount ELSE 0 END) AS credit,
SUM(CASE WHEN status='debit' THEN amount ELSE 0 END) AS debit
FROM Table
GROUP BY bank




thank u dude

Thanks

Go to Top of Page
   

- Advertisement -