开发者

Return multiple results depending on Quantity field

开发者 https://www.devze.com 2023-02-28 01:02 出处:网络
I have the following table of records: Product|Colour|Quantity --------------------------------------- Product A|Blue开发者_JAVA百科 |1

I have the following table of records:

Product     |    Colour  |   Quantity
---------------------------------------
Product A   |    Blue   开发者_JAVA百科 |          1
Product A   |    Red     |          2
Product B   |    Green   |          1

How can I write a query to return the following?:

Product     |    Colour
---------------------------
Product A   |    Blue
Product A   |    Red
Product A   |    Red
Product B   |    Green


WITH    numbers (rn) AS
        (
        SELECT  MAX(quantity)
        FROM    product
        UNION ALL
        SELECT  rn - 1
        FROM    numbers
        WHERE   rn > 1
        )
SELECT  p.*
FROM    product p
JOIN    numbers n
ON      p.quantity >= n.rn
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号