PostgreSQL:在表中的所有用户之间寻找共同的朋友

时间:2019-05-11 20:07:59

标签: postgresql mutual-friendship

我有一张这样的桌子:

create table a (
uid1 char(30),
uid2 char(30),
date_req date,
evnt char(30)
 );



insert into a (uid1, uid2, date_req, evnt) values ('abc', 'pqr', '1/1/2018', 'accept');
insert into a (uid1, uid2, date_req, evnt) values ('abc', 'abcd', '1/2/2018', 'accept');
insert into a (uid1, uid2, date_req, evnt) values ('abc', 'abce', '1/1/2018', 'accept');
insert into a (uid1, uid2, date_req, evnt) values ('abc', 'ftu', '1/4/2018', 'accept');
insert into a (uid1, uid2, date_req, evnt) values ('pqr', 'fuc', '1/1/2018', 'reject');
insert into a (uid1, uid2, date_req, evnt) values ('abcd', 'mno', '2/1/2018', 'reject');
insert into a (uid1, uid2, date_req, evnt) values ('abce', 'pqr', '1/5/2018', 'accept');
insert into a (uid1, uid2, date_req, evnt) values ('ftu', 'stu', '1/8/2018', 'reject');
insert into a (uid1, uid2, date_req, evnt) values ('fuc', 'vwx', '3/1/2018', 'accept');
insert into a (uid1, uid2, date_req, evnt) values ('mno', 'abc', '5/12/2018', 'accept');
insert into a (uid1, uid2, date_req, evnt) values ('pqr', 'abc', '1/13/2018', 'accept');
insert into a (uid1, uid2, date_req, evnt) values ('stu', 'abc', '1/16/2018', 'reject');
insert into a (uid1, uid2, date_req, evnt) values ('vwx', 'abc', '1/21/2018', 'reject');
insert into a (uid1, uid2, date_req, evnt) values ('abc', 'pqr', '1/1/2018', 'sent');
insert into a (uid1, uid2, date_req, evnt) values ('abc', 'abcd', '1/1/2018', 'sent');
insert into a (uid1, uid2, date_req, evnt) values ('abc', 'abce', '1/1/2018', 'sent');
insert into a (uid1, uid2, date_req, evnt) values ('abc', 'ftu', '1/3/2018', 'sent');
insert into a (uid1, uid2, date_req, evnt) values ('pqr', 'fuc', '1/1/2018', 'sent');
insert into a (uid1, uid2, date_req, evnt) values ('abcd', 'mno', '2/1/2018', 'sent');
insert into a (uid1, uid2, date_req, evnt) values ('abce', 'pqr', '1/3/2018', 'sent');
insert into a (uid1, uid2, date_req, evnt) values ('ftu', 'stu', '1/2/2018', 'sent');
insert into a (uid1, uid2, date_req, evnt) values ('fuc', 'vwx', '3/1/2018', 'sent');
insert into a (uid1, uid2, date_req, evnt) values ('mno', 'abc', '3/1/2018', 'sent');
insert into a (uid1, uid2, date_req, evnt) values ('pqr', 'abc', '1/1/2018', 'sent');
insert into a (uid1, uid2, date_req, evnt) values ('stu', 'abc', '1/12/2018', 'sent');
insert into a (uid1, uid2, date_req, evnt) values ('vwx', 'abc', '1/2/2018', 'sent');

我想认识唯一的uid1和uid2组合的所有共同朋友。例如:如果abc是与pqr的朋友,而pqr是与mno的朋友,那么mno和abc有1个共同朋友。同样,我想知道表中的每个用户。

预期结果示例将是这样(这是一个示例,不符合数据):

enter image description here

此外,状态从发送变为接受或拒绝的平均等待时间。

我该怎么做?

0 个答案:

没有答案