Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 根据数量字段返回多个结果_Sql Server_Sql Server 2005_Resultset - Fatal编程技术网

Sql server 根据数量字段返回多个结果

Sql server 根据数量字段返回多个结果,sql-server,sql-server-2005,resultset,Sql Server,Sql Server 2005,Resultset,我有以下记录表: Product | Colour | Quantity --------------------------------------- Product A | Blue | 1 Product A | Red | 2 Product B | Green | 1 如何编写查询以返回以下内容: Product | Colour ---------

我有以下记录表:

Product     |    Colour  |   Quantity
---------------------------------------
Product A   |    Blue    |          1
Product A   |    Red     |          2
Product B   |    Green   |          1
如何编写查询以返回以下内容:

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

这对我来说似乎是相反的结果。2个结果(数量为1)和1个结果(数量为2)。有什么想法吗?@Curt:当然,只要在子句中更改
的符号就行了。请看更新后的文章。这太棒了。我花了一段时间才弄明白它到底是如何工作的,但现在我知道了,想想它的伟大解决方案。干杯
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