什么是计算每个跟随者的二级追随者?

时间:2017-05-31 18:54:41

标签: sql-server

我试图理解什么是二级追随者的意思? 我有一个表格,后面有两列:followee和followers

Followee Follower
A        B
B        C
B        D
B        E
A        F
F        G
F        H
B        H

什么是找到第二学位的追随者?

1 个答案:

答案 0 :(得分:1)

要详细说明我的评论,您可以通过自我加入来看到这一点。

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 |
+----------+----------------------+----+