计算 SSIS 中的前十个类别

Calculating Top Ten Categories in SSIS

我正在使用 SSIS 包来每天更新我的内容。有数以千计的内容具有不同的审核 ID,我想计算每个审核 ID 的前十个类别。在我意识到我应该为每个 ModerationId 计算它之前,我使用这个查询来获取要更新的内容:

SELECT TOP 10 ModerationId, Category, COUNT(ContentSeqNum) AS Total FROM Content
WHERE Category IS NOT NULL
GROUP BY ModerationId, Category ORDER BY ModerationId, Total DESC

这是一个错误的方法,因为此查询计算所有数据的前十个类别,对于不同的 ModerationId 应该是不同的前十个类别。

如何更改此查询以计算每个 ModerationId 的前 10 个类别?

试试这个:

SELECT TOP(10) ModerationId, Category, COUNT(ContentSeqNum) OVER(PARTITION BY ModerationId ORDER BY ModerationId) AS Total 
FROM Content
WHERE Category IS NOT NULL
ORDER BY Total DESC

使用Row_number()函数

select * from
(
        select *, 
        row_number() over(partition by ModerationId order by ModerationId) as sno 
        from Content WHERE Category IS NOT NULL
) as t
where sno<=10

http://beyondrelational.com/modules/2/blogs/70/posts/10845/return-top-n-rows.aspx

查找更多方法

使用 Window Function 计算 Moderation ID 的前十名 categories。试试这个。

SELECT moderationid, 
       category, 
       total 
FROM   (SELECT Row_number() OVER (partition BY moderationid 
                   ORDER BY Count(contentseqnum)) Rn, 
               moderationid, 
               category, 
               Count(contentseqnum)               AS Total 
        FROM   content 
        WHERE  category IS NOT NULL 
        GROUP  BY moderationid, 
                  category) A 
WHERE  rn <= 10