2017-05-31 7 views
1

私は第二度の信者が何を意味するのか理解しようとしていますか? は、私は2つの列で、次の表を持っている:フォロイーやフォロワー各フォロワの第二次フォロワーは何をカウントしますか?

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

は、二度のフォロワーを見つけるために何ですか?

+4

BはAとCは、Bは、従ってCはA.実施例の第二度フォロワで以下続きます。 DはE、G、Hと同様にBのフォロワーであり、Aの2次フォロワでもあります。したがって、Aには5つの2次フォロワー(C、D、E、G、H)があります。 **これは宿題のような臭いです** – scsimon

+0

ありがとうございます.. – priya

答えて

1

私のコメントで詳しく説明すると、自己結合でこれを見ることができます。 `C> B> A`:

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 

RETURNS

+----------+----------------------+----+ 
| Followee | SecondDegreeFollower | CT | 
+----------+----------------------+----+ 
| A  | C     | 1 | 
| A  | D     | 1 | 
| A  | E     | 1 | 
| A  | G     | 1 | 
| A  | H     | 2 | 
+----------+----------------------+----+ 
関連する問題