Solved

MSSQL Query - Return Multiple Records for Each Row

Posted on 2012-03-20
4
359 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Creating and Managing Databases with phpMyAdmin in cPanel.
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…

743 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

11 Experts available now in Live!

Get 1:1 Help Now