0

我有一个查询,希望通过以下方式从SEDE中获取最受欢迎的问题

SELECT TOP 10
    'https://stackoverflow.com/questions/' + CONVERT(VARCHAR, Posts.Id) as url,
    (ViewCount / (SELECT - DATEDIFF(DAY, GETDATE(), CreationDate))) AS ave_views_per_day,
    Body as QUESTION,
    Posts.Id,
    Title
FROM
    Posts
LEFT JOIN 
    PostTags ON Posts.Id = PostTags.PostId
LEFT JOIN 
    Tags ON PostTags.TagId = Tags.Id
WHERE 
    TagName IN ('##tag##')
    AND ViewCount > 10000
ORDER BY 
    ave_views_per_day DESC

我有一个额外的查询来获得给定问题 ID 的最高得分答案:

SELECT 
    'https://stackoverflow.com/questions/' + CONVERT(VARCHAR, Id) as url, HighestScoringAnswer
FROM
    (SELECT TOP 1 
         Id, Body AS HighestScoringAnswer, 
         DENSE_RANK() OVER (ORDER BY Score DESC) Rank 
     FROM 
         (SELECT p.Id, p.ParentId, p.Body, p.Score
          FROM Posts p 
          WHERE p.ParentId = ##QuestionId##) x
     ORDER BY 
         Rank ASC) x

如何组合这些查询,以便##QuestionId##第二个查询的来源来自Posts.Id第一个查询,并将结果HighestScoringAnswer列添加到第一个查询的结果中?我尝试使用该Union操作,但无法弄清楚如何将 Id 从一个查询“传递”到另一个查询。

4

1 回答 1

0

您必须JOIN在两个查询(问题和答案)之间进行选择;不是UNIONUNION返回第一个查询的行加上第二个查询的行,不包括重复的行;但您想要前十个问题以及同一行中每个问题的得分更高的答案)。

试试这个查询,我认为这是您要查找的查询:

SELECT DISTINCT TOP 10
    'https://stackoverflow.com/questions/' + CONVERT(VARCHAR, q.Id) as url,
    (q.ViewCount / -DATEDIFF(DAY, GETDATE(), q.CreationDate)) AS ave_views_per_day,
    q.Body as QUESTION,
    q.Id,
    q.Title,
    FIRST_VALUE(a.Body) OVER (PARTITION BY q.id ORDER BY a.Score DESC) AS HighestScoringAnswer
    
FROM Posts q
LEFT JOIN Posts a ON q.id = a.ParentId

WHERE q.PostTypeId = 1 
      AND q.Tags LIKE '%postgresql%'
      AND q.ViewCount > 10000
      
ORDER BY ave_views_per_day DESC;
于 2021-10-12T11:45:53.843 回答