SQL 查询 Child Table 中的最大值
SQL Query With Max Value from Child Table
三个相关 tables:曲目(音乐曲目)、用户和关注。
下面的 table 是一个多对多关系,将用户(关注者)与用户(被关注者)联系起来。
我正在寻找这个作为最终结果:
<track_id>
、<user_id>
、<most popular followee>
前两列很简单,由曲目和用户之间的关系产生。第三个是我的问题。我可以加入以下 table 并获得每个用户关注的 all 的关注者,但是如何获得 only 最多的关注者关注人数最多的。
以下是 table 及其相关栏目:
tracks: id, user_id (fk to users.id), song_title
users: id
follows: followee_id (fk to users.id), follower_id (fk to users.id)
下面是一些示例数据:
TRACKS
1, 1, Some song title
USERS
1
2
3
4
FOLLOWS
2, 1
3, 1
4, 1
3, 4
4, 2
4, 3
DESIRED RESULT
1, 1, 4
对于期望的结果,第 3 个字段是 4,因为正如您在 FOLLOWS table 中看到的,用户 4 拥有最多的关注者。
我和身边的几位大神还在摸不着头脑。
这听起来像是 row_number()
的聚合查询。我对所有联接如何组合在一起感到有点困惑:
select t.*
from (select t.id, f.followee_id, count(*) as cnt,
row_number() over (partition by t.id order by count(*) desc) as seqnum
from followers f join
tracks t
on f.follow_id = t.user_id
group by t.id, f.followee_id
) t
where seqnum = 1;
所以我把它放到了 Linqpad 中,因为我更擅长使用 Linq。
Tracks
.Where(t => t.TrackId == 1)
.Select(t => new {
TrackId = t.TrackId,
UserId = t.UserId,
MostPopularFolloweeId = Followers
.GroupBy(f => f.FolloweeId)
.OrderByDescending(g => g.Count())
.FirstOrDefault()
.Key
});
生成的 SQL 查询如下(@p0 是曲目 ID):
-- Region Parameters
DECLARE @p0 Int = 1
-- EndRegion
SELECT [t0].[TrackId], [t0].[UserId], (
SELECT [t3].[FolloweeId]
FROM (
SELECT TOP (1) [t2].[FolloweeId]
FROM (
SELECT COUNT(*) AS [value], [t1].[FolloweeId]
FROM [Followers] AS [t1]
GROUP BY [t1].[FolloweeId]
) AS [t2]
ORDER BY [t2].[value] DESC
) AS [t3]
) AS [MostPopularFolloweeId]
FROM [Tracks] AS [t0]
WHERE [t0].[TrackId] = @p0
输出预期的响应,应该是更清晰查询的开始。
三个相关 tables:曲目(音乐曲目)、用户和关注。
下面的 table 是一个多对多关系,将用户(关注者)与用户(被关注者)联系起来。
我正在寻找这个作为最终结果:
<track_id>
、<user_id>
、<most popular followee>
前两列很简单,由曲目和用户之间的关系产生。第三个是我的问题。我可以加入以下 table 并获得每个用户关注的 all 的关注者,但是如何获得 only 最多的关注者关注人数最多的。
以下是 table 及其相关栏目:
tracks: id, user_id (fk to users.id), song_title
users: id
follows: followee_id (fk to users.id), follower_id (fk to users.id)
下面是一些示例数据:
TRACKS
1, 1, Some song title
USERS
1
2
3
4
FOLLOWS
2, 1
3, 1
4, 1
3, 4
4, 2
4, 3
DESIRED RESULT
1, 1, 4
对于期望的结果,第 3 个字段是 4,因为正如您在 FOLLOWS table 中看到的,用户 4 拥有最多的关注者。
我和身边的几位大神还在摸不着头脑。
这听起来像是 row_number()
的聚合查询。我对所有联接如何组合在一起感到有点困惑:
select t.*
from (select t.id, f.followee_id, count(*) as cnt,
row_number() over (partition by t.id order by count(*) desc) as seqnum
from followers f join
tracks t
on f.follow_id = t.user_id
group by t.id, f.followee_id
) t
where seqnum = 1;
所以我把它放到了 Linqpad 中,因为我更擅长使用 Linq。
Tracks
.Where(t => t.TrackId == 1)
.Select(t => new {
TrackId = t.TrackId,
UserId = t.UserId,
MostPopularFolloweeId = Followers
.GroupBy(f => f.FolloweeId)
.OrderByDescending(g => g.Count())
.FirstOrDefault()
.Key
});
生成的 SQL 查询如下(@p0 是曲目 ID):
-- Region Parameters
DECLARE @p0 Int = 1
-- EndRegion
SELECT [t0].[TrackId], [t0].[UserId], (
SELECT [t3].[FolloweeId]
FROM (
SELECT TOP (1) [t2].[FolloweeId]
FROM (
SELECT COUNT(*) AS [value], [t1].[FolloweeId]
FROM [Followers] AS [t1]
GROUP BY [t1].[FolloweeId]
) AS [t2]
ORDER BY [t2].[value] DESC
) AS [t3]
) AS [MostPopularFolloweeId]
FROM [Tracks] AS [t0]
WHERE [t0].[TrackId] = @p0
输出预期的响应,应该是更清晰查询的开始。