Sql server 如何计算每个追随者的二级追随者?
我想知道二级追随者是什么意思? 我有一个表,下面有两列:followee和followersSql server 如何计算每个追随者的二级追随者?,sql-server,Sql Server,我想知道二级追随者是什么意思? 我有一个表,下面有两列:followee和followers Followee Follower A B B C B D B E A F F G F H B H 怎样才能找到二级追随者?要详细说明我的评论,您可以通过自我连接看到这一点 declare @table table(Followee char(1), Follower char(1)) in
Followee Follower
A B
B C
B D
B E
A F
F G
F H
B H
怎样才能找到二级追随者?要详细说明我的评论,您可以通过自我连接看到这一点
declare @table table(Followee char(1), Follower char(1))
insert into @table
values
('A','B'),
('B','C'),
('B','D'),
('B','E'),
('A','F'),
('F','G'),
('F','H'),
('B','H')
select
l.Followee
,f.Follower as SecondDegreeFollower
,count(*) as CT
from
@table l
left join
@table f on f.Followee = l.Follower
where
f.Follower is not null
group by
l.Followee
,f.Follower
返回
+----------+----------------------+----+
| Followee | SecondDegreeFollower | CT |
+----------+----------------------+----+
| A | C | 1 |
| A | D | 1 |
| A | E | 1 |
| A | G | 1 |
| A | H | 2 |
+----------+----------------------+----+
B跟随A,C跟随B,因此C是A的二级跟随者。例如:
C>B>A
。D也是B的跟随者,因此也是a的二级跟随者,如E、G和H。因此a有5个二级跟随者(C、D、E、G、H)。这闻起来像是家庭作业谢谢我知道了。。