Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.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
Oracle SQL比较聚合行_Sql_Oracle - Fatal编程技术网

Oracle SQL比较聚合行

Oracle SQL比较聚合行,sql,oracle,Sql,Oracle,有点困在相对简单的SQL中 是否有人可以提出一些代码来检索aValue不同的聚合行(按GroupID分组)的GroupID? 例如,在下表中,我需要获取GroupID“4”,因为同一组(4)中的2个项目具有不同的aValue GroupId ItemID aValue 4 19 Hello 4 20 Hello1 5 78 Hello5 5 86 Hello5 您可以使用having子句查看不同值的计数

有点困在相对简单的SQL中

是否有人可以提出一些代码来检索aValue不同的聚合行(按GroupID分组)的GroupID? 例如,在下表中,我需要获取GroupID“4”,因为同一组(4)中的2个项目具有不同的aValue

GroupId ItemID  aValue
4       19      Hello 
4       20      Hello1
5       78      Hello5
5       86      Hello5

您可以使用having子句查看不同值的计数:

-- CTE for your sample data
with your_table (groupid, itemid, avalue) as (
  select 4, 19, 'Hello' from dual
  union all select 4, 20, 'Hello1' from dual
  union all select 5, 78, 'Hello5' from dual
  union all select 5, 86, 'Hello5' from dual
)
select groupid
from your_table
group by groupid
having count(distinct avalue) > 1;

   GROUPID
----------
         4
如果您确实还想查看各个值,可以在子查询中使用分析计数,并使用
where
而不是
have
对其进行过滤:

-- CTE for your sample data
with your_table (groupid, itemid, avalue) as (
  select 4, 19, 'Hello' from dual
  union all select 4, 20, 'Hello1' from dual
  union all select 5, 78, 'Hello5' from dual
  union all select 5, 86, 'Hello5' from dual
)
select groupid, itemid, avalue
from (
  select groupid, itemid, avalue,
    count(distinct avalue) over (partition by groupid) as value_count
  from your_table
)
where value_count > 1;

   GROUPID     ITEMID AVALUE
---------- ---------- ------
         4         19 Hello 
         4         20 Hello1
我会这样做:

select GroupId
from table t
group by GroupId
having min(aValue) <> max(aValue);

非常感谢亚历克斯。。你的第一个建议就足够了。我完全错过了有区别的方法。。。
select t.*
from table t
where exists (select 1  
              from table t1 
              where t1.GroupId = t.GroupId and 
                    t1.avalue <> t.avalue
             );