两个不同组中具有不同列值的记录数
Posted
技术标签:
【中文标题】两个不同组中具有不同列值的记录数【英文标题】:Number of records with different column values across two different groups 【发布时间】:2021-12-21 20:09:42 【问题描述】:我在 postgresql 中有下表,我们称之为table1
entity id | entity group | value |
---|---|---|
1 | A | 5000 |
2 | A | 4000 |
3 | A | 3000 |
4 | A | 2000 |
5 | A | 1000 |
1 | B | 5000 |
2 | B | 4800 |
3 | B | 2700 |
我有一个 hacky 方法如下
with entity_diff as (
with entitya as (
select entity_id,
value as value_group_a
from table1
where entity_group = 'A'
),
entityb as (
select entity_id,
value as value_group_b
from table1
where entity_group = 'B'
)
select entitya.entity_id,
entitya.value_group_a - entityb.value_group_b as value_diff
from entitya
inner join entityb on entitya.entity_id = entityb.entity_id
)
select count(*) from from entity_diff
where abs(entity_diff.value_diff) > 0;
有没有更简单的方法来获得这个答案,当我需要比较 3 或 4 个组时也可以扩展。
【问题讨论】:
【参考方案1】:您可以将count(distinct)
与子查询一起使用:
select count(*) from (select t1.id, count(distinct t1.value) h from table1 t1 group by t1.id) t2
where t2.h = (select count(*) from table1 t3 where t3.id = t2.id) and t2.h > 1;
输出:
count
-----
2
而对应的实体ID为(select t2.id from ...
):
id
-----
2
3
【讨论】:
【参考方案2】:您可以尝试以下使用方法:
-
带有从句的分组
在相似的
entity_id
s 上自我加入,但不同的entitygroup
s 具有不同的值。
查询 #1
select
count(1)
from (
select
entityid
from
table1
group by
entityid
having
count(distinct entitygroup) > 1 and
min(value) <> max(value)
) t1;
count |
---|
2 |
查询 #2
select
entityid
from
table1
group by
entityid
having
count(distinct entitygroup) > 1 and
min(value) <> max(value);
entityid |
---|
2 |
3 |
查询 #3
select
count(distinct t1.entityid)
from
table1 t1
inner join
table1 t2 on t1.entityid = t2.entityid and
t1.entitygroup < t2.entitygroup and
t1.value <> t2.value;
count |
---|
2 |
查询 #4
select
count(distinct t1.entityid)
from
table1 t1
inner join
table1 t2 on t1.entityid = t2.entityid and
t1.entitygroup < t2.entitygroup and
abs(t1.value - t2.value)>0;
count |
---|
2 |
查询 #5
select distinct
t1.entityid
from
table1 t1
inner join
table1 t2 on t1.entityid = t2.entityid and
t1.entitygroup < t2.entitygroup and
abs(t1.value - t2.value) > 0;
entityid |
---|
2 |
3 |
View working demo on DB Fiddle
【讨论】:
以上是关于两个不同组中具有不同列值的记录数的主要内容,如果未能解决你的问题,请参考以下文章