We help IT Professionals succeed at work.

SQL - Value as field header

holemania
holemania asked
on
I need help with a sql query.  I have the following and need to make it header a header field with that value.

ID      WAREHOUSE      QTY
AB1     SOUTHBLD        10
AB1      WESTBLD           2
AB1      NORTHBLD        4

Need it as followed:

ID      SOUTHBLD       WESTBLD      NORTHBLD     TOTAL Qty
AB1        10                      2                      4                16

How could I take the following sql query to get that?

Select Id, warehouse, qty from warehouse where id = 'AB1'
Comment
Watch Question

Commented:
Try:

Select ID, 
           Sum(Case When Warehouse = 'SOUTHBLD' then QTY else 0 end) AS SOUTHBLD,
           Sum(Case When Warehouse = 'WESTBLD' then QTY else 0 end) AS WESTBLD,
           Sum(Case When Warehouse = 'NORTHBLD' then QTY else 0 end) AS NORTHBLD,
           Sum(QTY) AS [Total Qty]
FROM TableName
WHERE ID = 'AB1'

Open in new window

CERTIFIED EXPERT
Top Expert 2010

Commented:
Are those the only three possible Warehouse values when ID = 'AB1'?

Author

Commented:
Thank you.  That worked.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.