Created
February 8, 2017 19:16
-
-
Save filipre/8fb9144ef468b44e7ffc0652f51f3b61 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with recursive | |
-- all friends into one direction | |
friends (a, b) as ( | |
values('Alice','Bob'), ('Alice','Carol'), ('Carol','Grace'), | |
('Carol','Chuck'), ('Chuck','Grace'),('Chuck','Anne'), | |
('Bob','Dan'),('Dan','Anne'),('Eve','Adam') | |
), | |
-- all friendships into both directions | |
friendship (name, friend) as ( | |
select a, b | |
from friends | |
union all | |
select b, a | |
from friends | |
), | |
chuck_nr (name, nr) as ( | |
select 'Chuck', 0 | |
union | |
select f.name, c.nr + 1 | |
from chuck_nr c, friendship f | |
where c.name = f.friend | |
and c.nr < 3 | |
and f.name not in (select t.name from chuck_nr t where t.nr < 2) | |
) | |
select name, min(nr) | |
from chuck_nr | |
group by name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment