【问题标题】: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
  • 我需要找出在不同实体组中具有不同值的实体的数量。
  • 在上面的示例中,答案将是 2 (entity_id: 2,3)。

我有一个 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 个组时也可以扩展。

【问题讨论】:

    标签: sql postgresql group-by


    【解决方案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】:

      您可以尝试以下使用方法:

      1. 带有从句的分组
      2. 在相似的entity_ids 上自我加入,但不同的entitygroups 具有不同的值。

      查询 #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

      【讨论】:

        猜你喜欢
        • 2023-01-09
        • 1970-01-01
        • 2019-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-15
        相关资源
        最近更新 更多