我正在尝试按月创建一系列顶级R帖子的时间序列。来自SE数据资源管理器的年份使用this query。这是我写的查询:
select Id, Title, Score, year(CreationDate) as creation_year,
month(CreationDate) as creation_month from Posts pp
inner join(max(Score) as max_score, year(CreationDate) as creation_year,
month(CreationDate) as creation_month from Posts
group by year(CreationDate), month(CreationDate)) grouped_pp
on pp.creation_year = grouped_pp.creation_year
and pp.creation_month = grouped_pp.creation_month
and pp.Score = grouped_pp.max_score
where tags = '<r>'
然而,这失败了,无益:
&#39;,&#39;附近的语法不正确。
不确定如何修复此查询。
答案 0 :(得分:1)
您在select
之后错过了join
;
select
Id,
Title,
Score,
year(CreationDate) as creation_year,
month(CreationDate) as creation_month
from Posts pp
inner join (
select
max(Score) as max_score,
year(CreationDate) as creation_year,
month(CreationDate) as creation_month
from Posts
group by year(CreationDate), month(CreationDate)
) grouped_pp
on year(pp.CreationDate) = grouped_pp.creation_year
and month(pp.CreationDate) = grouped_pp.creation_month
and pp.Score = grouped_pp.max_score
where tags = '<r>'
您必须将pp.creation_year
修改为year(pp.CreationDate)
[和month
相同]才能使查询正常工作
要获得所需的结果,您可能需要使用另外两个表格,例如PostTags
和tags
;
select
Id,
Title,
Score,
creation_year,
creation_month
from (
select
p.Id,
p.Title,
p.Score,
year(p.CreationDate) as creation_year,
month(p.CreationDate) as creation_month,
rank() over(partition by year(p.CreationDate), month(p.CreationDate) order by p.Score desc) rnk
from Posts p
join PostTags pt on pt.postid = p.Id
join tags t on t.id = pt.tagid
where t.tagname = 'r'
) x
where rnk = 1
order by creation_year, creation_month
以下是link