我需要一个查询来从StackExchange数据集中获取结果,以便为每个用户返回100个顶级信誉用户,帖子不超过3个帖子(帖子的主体)。表格如下:
用户:(id,声誉) 帖子:(id,OwnerUserId,body)
我写了以下查询:
var mystr = 'public\uploads\file-1490095922739.jpg';
var filename = mystr.replace('public\uploads', '');
它会返回每个用户的所有帖子,但我想要的是每个用户只能获得3个帖子(正文)。请帮忙!
答案 0 :(得分:1)
您可以使用cross apply()
:
select
u.rank
, u.Id as [User Link]
, u.Reputation
, p.Body
from (
select top 100 *
, row_number() over (
order by u.Reputation desc
) as rank
from Users u
order by u.Reputation desc
) as u
cross apply (
select top 3
p.body
from Posts p
where p.OwnerUserId = u.Id
) p
order by u.Reputation desc
演示:https://data.stackexchange.com/stackoverflow/revision/645393/805819/