基于不同列的列重叠
Overlap of a column based on a different column
我在 Postgres 中有一个简单的 table,其中包含星期几和人员。
INSERT INTO mytable (day, person)
values
('Monday', 'A'),
('Monday', 'A'),
('Monday', 'B'),
('Tuesday', 'A'),
('Wednesday', 'A'),
('Wednesday', 'B'),
('Thursday', 'B'),
('Thursday', 'B');
我想找出每一天重叠的人数。因此,例如,星期一有 A 和 B。星期二只有 B,因此会产生如下一行:
('Monday', 'Tuesday', 1)
最终输出应如下所示:
('Monday', 'Monday', 2),
('Monday', 'Tuesday', 1),
('Monday', 'Wednesday', 2),
('Monday', 'Thursday', 1),
('Tuesday', 'Tuesday', 1),
('Tuesday', 'Monday', 1),
('Tuesday', 'Wednesday', 1),
('Tuesday', 'Thursday', 0),
('Wednesday', 'Wednesday', 2),
('Wednesday', 'Monday', 2),
('Wednesday', 'Tuesday', 1),
('Wednesday', 'Thursday', 1),
('Thursday', 'Thursday', 1),
('Thursday', 'Monday', 1),
('Thursday', 'Tuesday', 0),
('Thursday', 'Wednesday', 1)
我一直在尝试交叉连接和相交,但似乎没有产生正确的结果。
使用自连接:
select t1.day, t2.day, count(distinct t2.person) as num_overlaps
from mytable t1 join
mytable t2
on t1.person = t2.person
group by t1.day, t2.day
order by t1.day, t2.day;
编辑:
我看到你也想包括零重叠。这有点棘手。为此,您需要生成所有天的组合,然后加入数据:
select d1.day, d2.day, count(distinct t2.person)
from (select distinct day from mytable) d1 cross join
(select distinct day from mytable) d2 left join
mytable t1
on t1.day = d1.day left join
mytable t2
on t2.day = d2.day and t2.person = t1.person
group by d1.day, d2.day
order by d1.day, d2.day;
Here 是一个 db<>fiddle.
我在 Postgres 中有一个简单的 table,其中包含星期几和人员。
INSERT INTO mytable (day, person)
values
('Monday', 'A'),
('Monday', 'A'),
('Monday', 'B'),
('Tuesday', 'A'),
('Wednesday', 'A'),
('Wednesday', 'B'),
('Thursday', 'B'),
('Thursday', 'B');
我想找出每一天重叠的人数。因此,例如,星期一有 A 和 B。星期二只有 B,因此会产生如下一行:
('Monday', 'Tuesday', 1)
最终输出应如下所示:
('Monday', 'Monday', 2),
('Monday', 'Tuesday', 1),
('Monday', 'Wednesday', 2),
('Monday', 'Thursday', 1),
('Tuesday', 'Tuesday', 1),
('Tuesday', 'Monday', 1),
('Tuesday', 'Wednesday', 1),
('Tuesday', 'Thursday', 0),
('Wednesday', 'Wednesday', 2),
('Wednesday', 'Monday', 2),
('Wednesday', 'Tuesday', 1),
('Wednesday', 'Thursday', 1),
('Thursday', 'Thursday', 1),
('Thursday', 'Monday', 1),
('Thursday', 'Tuesday', 0),
('Thursday', 'Wednesday', 1)
我一直在尝试交叉连接和相交,但似乎没有产生正确的结果。
使用自连接:
select t1.day, t2.day, count(distinct t2.person) as num_overlaps
from mytable t1 join
mytable t2
on t1.person = t2.person
group by t1.day, t2.day
order by t1.day, t2.day;
编辑:
我看到你也想包括零重叠。这有点棘手。为此,您需要生成所有天的组合,然后加入数据:
select d1.day, d2.day, count(distinct t2.person)
from (select distinct day from mytable) d1 cross join
(select distinct day from mytable) d2 left join
mytable t1
on t1.day = d1.day left join
mytable t2
on t2.day = d2.day and t2.person = t1.person
group by d1.day, d2.day
order by d1.day, d2.day;
Here 是一个 db<>fiddle.