1

我有以下最终选择语句:

SELECT   ReportingDate
         ,PortfolioID
         ,IssueNme
         ,ROW_NUMBER() OVER (PARTITION BY PortfolioID ORDER BY SUM(Percentage) DESC) AS [Rank]
         ,CAST(SUM(Percentage) AS DECIMAL(22,1)) AS [Weight]
FROM     @Worktable as WT
WHERE    WT.IssueType1 IN ('020')
GROUP BY WT.ReportingDate, WT.PortfolioID, WT.IssueNme
ORDER BY [Weight] DESC

但是,我只想查看每个组合 ID 的前 10 名。我将如何做到这一点,以确保我获得分组的每个 PortfolioID 的前 10 名?

4

1 回答 1

1

您可以使用CTE. 我会RANK改用,但是每组你可能会得到超过 10 个。

WITH CTE AS
(
  SELECT 
      RANK() OVER (PARTITION BY PortfolioID ORDER BY SUM(Percentage) DESC) AS [RANK]
  ,   ReportingDate
  ,   PortfolioID
  ,   IssueNme
  ,   CAST(SUM(Percentage) AS DECIMAL(22,1))  AS [Weight]
  FROM @Worktable as WT
  WHERE WT.IssueType1 IN ('020')
  GROUP BY WT.ReportingDate, WT.PortfolioID, WT.IssueNme
)
SELECT 
  ReportingDate
, PortfolioID
, IssueNme
, [Weight]
FROM CTE
WHERE [RANK] <= 10
ORDER BY [Weight] DESC

排名功能

于 2012-11-20T12:41:03.350 回答