SSRS排名前20位,适用于多个地点

时间:2014-05-09 15:19:05

标签: sql reporting-services

我正在尝试过滤SSRS报告,以显示每个位置为多个位置销售的前20个项目。我有查询拉出按desc sum(数量)排序的所有商品,并且还有查询拉行号,以为我可以在SSRS中过滤到前20行号。问题是,这会过滤整个数据集而不是每个位置。

查询:

SELECT
  [Location]
  ,[Item No_]
  ,sum(-[Quantity]) as TotalQuantity
  ,ROW_NUMBER() OVER (PARTITION BY [Location Code] ORDER BY sum(-[Quantity]) DESC) AS RN
FROM [Ledger Entry]
  left join [Item] i on  [Ledger Entry].[Item Number] = i.No_
  WHERE 
  [Date] BETWEEN @StartDate and @EndDate
  GROUP BY 
  [Location Code]
  ,[Item No_] 

很抱歉,如果代码不好,我对SQL很新。

1 个答案:

答案 0 :(得分:0)

您在上面的代码中有查询的基础。这应该为每个位置提供1到n的计数。这将成为您从中选择记录的子查询。示例如下:

SELECT  [Location] ,[Item No_] ,TotalQuantity

FROM
(
SELECT
     [Location]
    ,[Item No_]
    ,sum(-[Quantity])                       AS  TotalQuantity
    ,ROW_NUMBER()
        OVER (PARTITION BY [Location Code] 
        ORDER BY sum(-[Quantity]) DESC)     AS RN
FROM 
    [Ledger Entry]

    LEFT JOIN [Item] i 
    ON  [Ledger Entry].[Item Number] = i.No_
WHERE 
  [Date] BETWEEN @StartDate and @EndDate
GROUP BY 
   [Location Code]
  ,[Item No_] 
) AS SubQuery

WHERE RN <= 20

将此有限的结果集传递给SSRS将只为您提供每个位置的前20个项目。它通常更好地限制和过滤数据查询而不是SSRS。