Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.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
在MS SQL中由具有重复值的多个列进行分区_Sql_Sql Server - Fatal编程技术网

在MS SQL中由具有重复值的多个列进行分区

在MS SQL中由具有重复值的多个列进行分区,sql,sql-server,Sql,Sql Server,例如,我在一个表中有两列 **Fruit Color** Mango Yellow Mango Yellow Apple Red Apple Red 预期产量 **Rank Fruit Color** 1 Mango Yellow 1 Mango Yellow 2 Apple Red 2 Apple Red 尝试了Row_number(),但似乎没有产生预期的输出。也尝试了Rank()和Dense_Rank(),但没有得到预期的结果 SELECT ROW_NUMBER

例如,我在一个表中有两列

**Fruit Color**
Mango Yellow
Mango Yellow
Apple Red
Apple Red
预期产量

**Rank Fruit Color**
1    Mango Yellow
1    Mango Yellow
2    Apple Red
2    Apple Red
尝试了Row_number(),但似乎没有产生预期的输出。也尝试了Rank()和Dense_Rank(),但没有得到预期的结果

SELECT ROW_NUMBER() OVER (PARTITION BY Fruit,Color order by Fruit) , Fruit,Color
from #temp
ORDER BY FRUIT

rwno    Fruit   Color
1   Apple   Red
2   Apple   Red
1   Mango   Yellow
2   Mango   Yellow
您需要
densite\u rank()
分区:

SELECT DENSE_RANK() OVER (ORDER BY Fruit, Color), Fruit, Color
from #temp
ORDER BY FRUIT
您需要
densite\u rank()
分区:

SELECT DENSE_RANK() OVER (ORDER BY Fruit, Color), Fruit, Color
from #temp
ORDER BY FRUIT

作品谢谢,很好!非常感谢。