Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在组内比较以获取公共值_Sql_Oracle - Fatal编程技术网

Sql 在组内比较以获取公共值

Sql 在组内比较以获取公共值,sql,oracle,Sql,Oracle,我有一张表,上面有以下数据 案例1: table1 ------------- id type ------------- 1 X 1 Y 2 X 3 Z 3 X ------------- 现在,正如您所看到的,X对于所有id都是公共的,所以在本例中,我需要返回X 案例2: table1 ------------- id type ------------- 1 X 1

我有一张表,上面有以下数据

案例1:

table1
-------------
id      type
-------------
1         X
1         Y
2         X
3         Z
3         X
-------------
现在,正如您所看到的,X对于所有id都是公共的,所以在本例中,我需要返回X

案例2:

table1
-------------
id      type
-------------
1         X
1         Y
2         X
2         Y
3         X
3         Y
--------------
在这种情况下,X和Y都是公共的,然后我需要返回X和Y逗号分隔的X,Y

案例3

table1
-------------
id      type
-------------
1         X
1         Y
2         X
2         Y
3         X
3         Y
4         NULL
------------------
如果任何记录中出现null,我需要返回null

事实上,我告诉你的数据是从3个表中填充的,所以我已经为此编写了查询,但是现在我需要比较组中的公共数据组,这让我很困惑,如何比较组

注意:此处的组基于ID


任何帮助都将被告知

您可以将发生的事件与ID的计数进行比较吗

with data as (select rownum id, 'X' type from dual connect by level <= 3
              union all
              select rownum id, 'Y' type from dual connect by level <= 3
              union all
              select 3 id, 'Z' type from dual)
select wm_concat(distinct type)
  from (select type, count(*) over (partition by type) cnt, count(distinct id) over () total_ids
          from data)
 where cnt = total_ids;
而不是4,NULL,并且还希望返回NULL而不是分隔列表在这种情况下,您可以添加一个带有4的检查,上面的NULL将返回NULL,即使在以前的SQL版本上,因为计数不会占用:

       with data as (select rownum id, 'X' type from dual connect by level <= 3
                      union all
                      select rownum id, 'Y' type from dual connect by level <= 3
                      union all
                      select 3 id, 'Z' type from dual)
        select wm_concat(distinct type)
          from (select type, count(*) over (partition by type) cnt, count(distinct id) over 

() total_ids,
                       max(case when type is null then 'Y' else 'N' end) over () has_null_in_set
                  from data)
         where cnt = total_ids
          and has_null_in_set = 'N';

:不,您的编辑部分不是我的有效案例,但非常感谢您的精彩回答,非常感谢
       with data as (select rownum id, 'X' type from dual connect by level <= 3
                      union all
                      select rownum id, 'Y' type from dual connect by level <= 3
                      union all
                      select 3 id, 'Z' type from dual)
        select wm_concat(distinct type)
          from (select type, count(*) over (partition by type) cnt, count(distinct id) over 

() total_ids,
                       max(case when type is null then 'Y' else 'N' end) over () has_null_in_set
                  from data)
         where cnt = total_ids
          and has_null_in_set = 'N';