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
 General SQL Server Forums
 New to SQL Server Programming
 select query

Author  Topic 

joelseverich
Starting Member

34 Posts

Posted - 2010-04-26 : 11:10:13
Hi.
I have a table with columns (code, name, cost, type)

type can be 'A' or 'B', now how can i select cost as costA when type is 'A' and costB when type is 'B'

please help me on this.

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2010-04-26 : 11:15:15
SELECT
Code,
Name,
Type,
CASE WHEN Type = 'A' THEN cost ELSE 0 END AS CostA,
CASE WHEN Type = 'B' THEN cost ELSE 0 END AS CostB
FROM Table
Go to Top of Page

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-26 : 11:16:40
[code]SELECT code, name,
CASE WHEN Type = 'A' THEN cost ELSE NULL END AS CostA,
CASE WHEN Type = 'B' THEN cost ELSE NULL END AS CostB
FROM tableName[/code]

------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.
Go to Top of Page

joelseverich
Starting Member

34 Posts

Posted - 2010-04-26 : 11:22:30
it worked perfectly, thanks a lot
Go to Top of Page
   

- Advertisement -