Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何计算每个追随者的二级追随者?_Sql Server - Fatal编程技术网

Sql server 如何计算每个追随者的二级追随者?

Sql 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和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))
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)。这闻起来像是家庭作业谢谢我知道了。。