SQL - Value as field header

Posted on 2012-08-16
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.

AB1     SOUTHBLD        10
AB1      WESTBLD           2
AB1      NORTHBLD        4

Need it as followed:

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'
Question by:holemania
    LVL 9

    Accepted Solution


    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

    LVL 92

    Expert Comment

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

    Author Closing Comment

    Thank you.  That worked.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
    Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now