Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.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_Sql Server_Tsql_Row Number - Fatal编程技术网

SQL Server,使用行号

SQL Server,使用行号,sql,sql-server,tsql,row-number,Sql,Sql Server,Tsql,Row Number,我有一个下面的查询,它返回一个按售出单位数量订购的售出商品列表。卖出最多的商品排在第一位,随后的商品按升序排列 SELECT RANK() OVER (ORDER BY SUM(Quantity) DESC, SUM(LineTotalInDefaultCurrency) DESC) AS SalesRank, P.Name ,SUM(Quantity) as UnitsSold , SUM(LineTotalInDefaultC

我有一个下面的查询,它返回一个按售出单位数量订购的售出商品列表。卖出最多的商品排在第一位,随后的商品按升序排列

SELECT  
      RANK() OVER (ORDER BY SUM(Quantity) DESC,       
      SUM(LineTotalInDefaultCurrency) DESC) AS SalesRank,
      P.Name ,SUM(Quantity) as UnitsSold ,
      SUM(LineTotalInDefaultCurrency) as RevenueDefaultCurrency, 
      sf.ProductId
FROM 
      SalesFact sf 
INNER JOIN 
      Product p ON sf.ProductId = p.ProductId 
WHERE 
      Dttm >= '2014-08-08' AND Dttm <= '2017-08-09' GROUP BY sf.ProductId, p.Name
我正在尝试向结果查询中添加一个行号,以便我的结果如下所示

Row SalesRank   Name                                UnitsSold   RevenueDefaultCurrency  ProductId   
1   1           Energy Saving Dryer Balls           1230        6429.58         1086381     
2   2           Universal Dishwasher Cutlery Basket 654         4700.64         1107301     
3   3           Limescale and Detergent Remover     361         4106.00         664212      
4   4           Universal Extendable Oven Shelf     364         3885.77         655005  
5   5           2500 Watt Fan Oven Element          157         1532.72         1019719 
6   6           Filter Vacuum Bags NVM-1CH          273         2320.88         479302  
7   7           Universal Dishwasher Cutlery Basket 81          1954.66         511673  
8   8           Ice Cube Tray                       10          20.99           655045
9   8           Vacuum Filter - Pack of 2           10          20.99           470556
10  8           Vacuum Post Motor Filter            10          20.99           1562181
我一直在尝试使用ROW_NUMBER来实现这一点。目前,我已经修改了我的查询,以便包含行\号,所以现在我有了

SELECT  
     RANK() OVER (ORDER BY SUM(Quantity) DESC, 
     SUM(LineTotalInDefaultCurrency) DESC) AS SalesRank,
     p.Name ,SUM(Quantity) as UnitsSold ,
     SUM(LineTotalInDefaultCurrency) as RevenueDefaultCurrency,      
     sf.ProductId,
     ROW_NUMBER() OVER(ORDER BY COUNT(sf.ProductId) DESC, sf.ProductId) AS [row]
 FROM 
     SalesFact sf INNER JOIN Product p ON sf.ProductId = p.ProductId 
 WHERE 
     Dttm >= '2014-08-08' AND Dttm <= '2017-08-09' 
 GROUP BY 
     sf.ProductId, p.Name
然而,我不能得到正确的订购。如果我按行添加ORDER,则SalesRank是无序的,如果我按SalesRank添加ORDER,则结果不是按行排序的

我希望这是有道理的。有谁能告诉我怎样才能得到像上面这样的结果集吗。
感谢您所需的结果集,您似乎希望行号的顺序相同,但在相同的排名内,按名称的其他顺序:

with cte as
(
SELECT  
   p.Name ,
   SUM(Quantity) as UnitsSold ,
   SUM(LineTotalInDefaultCurrency) as RevenueDefaultCurrency,  
   sf.ProductId
FROM 
   SalesFact sf 
INNER JOIN 
    Product p ON sf.ProductId = p.ProductId 
WHERE 
    Dttm >= '2014-08-08' AND Dttm <= '2017-08-09' 
GROUP BY 
    sf.ProductId, p.Name
)

select *,    
       RANK() OVER (ORDER BY UnitsSold DESC, RevenueDefaultCurrency DESC) AS SalesRank,
       ROW_NUMBER() OVER (ORDER BY UnitsSold DESC, RevenueDefaultCurrency desc, Name ) AS [row] 
from cte;
我认为您的行定义不正确:

SELECT ROW_NUMBER() OVER (ORDER BY SUM(Quantity) DESC, 
                                   SUM(LineTotalInDefaultCurrency) DESC
                         ) AS row,
       RANK() OVER (ORDER BY SUM(Quantity) DESC, 
                             SUM(LineTotalInDefaultCurrency) DESC
                   ) AS SalesRank,
       p.Name, SUM(Quantity) as UnitsSold ,
       SUM(LineTotalInDefaultCurrency) as RevenueDefaultCurrency,      
       sf.ProductId,   
FROM SalesFact sf INNER JOIN
     Product p
     ON sf.ProductId = p.ProductId 
WHERE Dttm >= '2014-08-08' AND Dttm <= '2017-08-09' 
GROUP BY sf.ProductId, p.Name
ORDER BY [row];

您的查询没有ORDER BY,因此引擎可以按照它喜欢的任何顺序显示行,即使您添加了行号。如果按SUMQuantity DESC、SUMLineTotalInDefaultCurrency DESC在订单上添加行\ u编号作为行,您将获得与排名相同的顺序,如果您愿意,您可以添加分段符。然后将整个内容包装为子查询并按行排序。或者直接按排名排序,如果你真的不需要行号,只需要有一个顺序。谢谢,这几乎是我所需要的,但是它会返回不同数量的结果。您的查询返回20行,您是对的,行数和排名应根据分组计算,我更新了我的答案
SELECT ROW_NUMBER() OVER (ORDER BY SUM(Quantity) DESC, 
                                   SUM(LineTotalInDefaultCurrency) DESC
                         ) AS row,
       RANK() OVER (ORDER BY SUM(Quantity) DESC, 
                             SUM(LineTotalInDefaultCurrency) DESC
                   ) AS SalesRank,
       p.Name, SUM(Quantity) as UnitsSold ,
       SUM(LineTotalInDefaultCurrency) as RevenueDefaultCurrency,      
       sf.ProductId,   
FROM SalesFact sf INNER JOIN
     Product p
     ON sf.ProductId = p.ProductId 
WHERE Dttm >= '2014-08-08' AND Dttm <= '2017-08-09' 
GROUP BY sf.ProductId, p.Name
ORDER BY [row];