Solved

MSSQL Query - Return Multiple Records for Each Row

Posted on 2012-03-20
4
363 Views
Last Modified: 2012-03-21
Hi Experts,

I have a table set up similar to the following

ID | Order | Qty1 | Qty2 | Qty3 | Qty4

I need a query to return four records for each row like this:

ID | Order | Qty1
ID | Order | Qty2
ID | Order | Qty3
ID | Order | Qty4

but only return those whose quantity is not zero

Thanks
-Jeremy
0
Comment
Question by:FamousMortimer
  • 2
4 Comments
 
LVL 25

Expert Comment

by:jogos
ID: 37742114
select id,order ,qty1 from tableA when  qty1 is not null
union all
select id,order ,qty2 from tableA when  qty2 is not null
union all
select id,order ,qty3 from tableA when  qty3 is not null
union all
select id,order ,qty4 from tableA when  qty4 is not null

Open in new window

0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 37742403
SELECT
    ID, Order,
    CASE whichQty
        WHEN 1 THEN Qty1
        WHEN 2 THEN Qty1
        WHEN 3 THEN Qty1
        WHEN 4 THEN Qty1
    END AS Qty
FROM dbo.tablename
CROSS JOIN (
    SELECT 1 AS whichQty UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL
    SELECT 4
) AS whichQtys
WHERE
    CASE whichQty
        WHEN 1 THEN Qty1
        WHEN 2 THEN Qty1
        WHEN 3 THEN Qty1
        WHEN 4 THEN Qty1
    END <> 0
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 37742441
That avoids scanning the table multiple times.

But UNPIVOT will probably have the best performance.


SELECT
    ID, [Order], Qty
FROM (
    SELECT ID, [Order], Qty1, Qty2, Qty3, Qty4
    FROM dbo.tablename
) AS base_data
UNPIVOT (
    Qty FOR Qty_List IN
       ([Qty1], [Qty2], [Qty3], [Qty4])
    )
AS unpvt
0
 
LVL 10

Author Closing Comment

by:FamousMortimer
ID: 37748267
Exactly what I was looking for... I know I could have used Unions but this was a quick, powerful, and elegant solution.

I was not familiar with unpivot, so thanks a lot!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Read about achieving the basic levels of HRIS security in the workplace.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

911 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

24 Experts available now in Live!

Get 1:1 Help Now