[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL - Value as field header

Posted on 2012-08-16
3
Medium Priority
?
362 Views
Last Modified: 2012-08-24
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'
0
Comment
Question by:holemania
3 Comments
 
LVL 9

Accepted Solution

by:
McOz earned 2000 total points
ID: 38302444
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

0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 38303079
Are those the only three possible Warehouse values when ID = 'AB1'?
0
 

Author Closing Comment

by:holemania
ID: 38331238
Thank you.  That worked.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Integration Management Part 2
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question