如何对十个最常用的标签(在SEDE中)进行SQL十个最不赞成的问题?

时间:2019-03-23 20:55:50

标签: tsql sql-server-2017 dataexplorer

Stack Exchange Data Explorer(SEDE)中,我试图为十个最常用的标签(即JavaScript,HTML等)获得十个最不赞成的问题。

我了解如何获得最常用的标签:

select Id, TagName, Count
from Tags t
order by count desc

现在,对于每个标签,我都想提出十个最受好评的问题。我想我需要某种加入方式。问题是在Posts表中,Tags是一个数组。

1 个答案:

答案 0 :(得分:0)

请参阅the SEDE schema。使用PostTags表将标签链接到问题。

然后,您可以使用ROW_NUMBER()来对每个标签的前10个问题进行排名。

这是一种方法(See it live in SEDE):

WITH topTags AS (
    SELECT TOP 10
                t.Id,
                t.TagName,
                t.Count,
                tagRank = ROW_NUMBER() OVER (ORDER BY t.Count DESC)
    FROM        Tags t
    ORDER BY    t.Count DESC
)
SELECT
        qbt.TagName     AS [Tag],
        --qbt.tagRank     AS [Tag Rank],
        qbt.Count       AS [Q's for Tag],
        qbt.Score       AS [Qst Score],
        qbt.Id          AS [Post Link],
        qbt.qstRow      AS [Rank in tag]
FROM (
    SELECT
                tt.TagName,
                --tt.tagRank,
                tt.Count,
                q.Score,
                q.Id,
                qstRow = ROW_NUMBER() OVER (PARTITION BY tt.Id ORDER BY tt.Id, q.Score DESC)
    FROM        topTags     tt
    LEFT JOIN   PostTags    pt  ON  pt.TagId = tt.Id
    LEFT JOIN   Posts       q   ON  q.ID = pt.PostId
) qbt
WHERE       qbt.qstRow <= 10
ORDER BY    qbt.Count DESC,
            qbt.Score DESC