我试图判断特定帐号和不同组织的输出是否相同,并在Oracle中显示差异

假设我有一个名为myTable的表,它包含两个字段:accountnumberorg_id

只有3org_id个:81281和404个

image

数据集:

|accountnumber|org_id|
|4435354      |81    |
|4435354      |281   |
|4435354      |404   |
|3333333      |81    |
|3333333      |281   |
|4444444      |81    |
|4444444      |81    |
|4444444      |281   |
|4444444      |404   |

我想找到所有不同的帐户,没有每个组织id的确切金额

例如:

账户4435354有一行用于组织id 81,一行用于组织id 281和一行

帐户3333333有一行用于组织id 81,一行用于组织id 281,但没有

账户44444有两行用于组织标识81,一行用于组织标识281和一行

DESIRED OUTPUT:

ACCOUNTNUMBER| ORG_ID|COUNT(*)
|3333333      |  81   |1
|3333333      |  281  |1
|3333333      |  404  |0
|4444444      |  81   |2
|4444444      |  281  |1
|4444444      |  404  |1

我怎样才能在甲骨文中实现这样的目标?


编辑#1

此场景是有效的,不应显示

|9999999      |81    |
|9999999      |81    |
|9999999      |81    |
|9999999      |404   |
|9999999      |404   |
|9999999      |404   |
|9999999      |281   |
|9999999      |281   |
|9999999      |281   |

这是正确的,因为该帐号的每个组织id中都有3个,这是正确的

推荐答案

问题:更新DBFiddle

select *
from (
    select 
        v.*
        ,min(cnt_org)over(partition by accountnumber) min_cnt_org
        ,max(cnt_org)over(partition by accountnumber) max_cnt_org
    from (
        select
          accountnumber
          ,org_id
          ,count(org_id)          over(partition by accountnumber) cnt
          ,count(distinct org_id) over(partition by accountnumber) cnt_distinct
          ,count(*)               over(partition by accountnumber,org_id) cnt_org
          ,listagg(org_id,',')within group(order by org_id)
             over(partition by accountnumber)
               as orgs
          ,listagg(distinct org_id,',')within group(order by org_id) 
             over(partition by accountnumber)
               as orgs_distinct
        from mytable
        ) v
    ) v2
where cnt_distinct<>3
or min_cnt_org!=max_cnt_org;

结果:

ACCOUNTNUMBER     ORG_ID        CNT CNT_DISTINCT    CNT_ORG ORGS          ORGS_DISTINCT  MIN_CNT_ORG MAX_CNT_ORG
------------- ---------- ---------- ------------ ---------- ------------- -------------- ----------- -----------
      3333333         81          2            2          1 81,281        81,281                   1           1
      3333333        281          2            2          1 81,281        81,281                   1           1
      4444444         81          4            3          2 81,81,281,404 81,281,404               1           2
      4444444         81          4            3          2 81,81,281,404 81,281,404               1           2
      4444444        281          4            3          1 81,81,281,404 81,281,404               1           2
      4444444        404          4            3          1 81,81,281,404 81,281,404               1           2

Sql相关问答推荐

基于时间的SQL聚合

SUM(条件)在Oracle?

从2个表中查找每条记录的唯一最接近的日期匹配

为什么在这种情况下我不能使用LAG函数?

如何在Presto中将多个列合并到一个数组中

使用SQL创建列出两个时间戳之间小时数的列

PostgreSQL抽奖查询

将所有XML文件导入到SQL Server中

合并分层表SQL中的第一个非空、变化的空位置

从单个表达式中的分隔字符串中取平均值

为什么左联接结果在MS Access数据库中不匹配

使用 SQL 将列添加到 Access 数据库时出错

将 json 列键映射到第二个表中的匹配列值

将varchar (7)列转换为datetime

基于字符串的SQL查询

计算 SQL 中的总体成功率:递归 CTE 还是替代方法?

如何对 SQL 表中的连续时间戳进行分组?

SQLite 中的过滤运行总和视图

SQL查询以获取从特定可变日期看到的用户

如果当前日期是一周中的某一天,则从另一天提取结果