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
精彩评论