开发者

Return multiple results depending on Quantity field

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜