将两个 sql 查询合并为一个查询,以从 SEDE 中获取单个 table 中最受欢迎的问题和答案
Combining two sql queries into one to get most popular questions & answers in single table from SEDE
我想从SEDE
中获取最热门的问题
SELECT TOP 10
'https://whosebug.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://whosebug.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 从一个查询“传递”到另一个查询。
你必须在两个查询(问题和答案)之间做一个 JOIN
;不是 UNION
(UNION
return 来自第一个查询的行加上来自第二个查询的行,不包括重复的行;但是您想要前十个问题和每个问题的得分更高的答案在同一行)。
试试这个查询,我想这就是你要找的查询:
SELECT DISTINCT TOP 10
'https://whosebug.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;
我想从SEDE
中获取最热门的问题SELECT TOP 10
'https://whosebug.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://whosebug.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 从一个查询“传递”到另一个查询。
你必须在两个查询(问题和答案)之间做一个 JOIN
;不是 UNION
(UNION
return 来自第一个查询的行加上来自第二个查询的行,不包括重复的行;但是您想要前十个问题和每个问题的得分更高的答案在同一行)。
试试这个查询,我想这就是你要找的查询:
SELECT DISTINCT TOP 10
'https://whosebug.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;