Sql 如何使用';分组依据';具有以下查询的函数

Sql 如何使用';分组依据';具有以下查询的函数,sql,oracle,group-by,Sql,Oracle,Group By,在下面的代码中,我只想在GROUPBY条件中给出的三列上应用group函数。但它给出了错误的、错误数量的论点 select a.id,a.name, c.code, a.active_dt, a.inactive_dt, b.category, count(a.id,a.name,c.code) count from student a,class b, descrip c where a.id=b.id and a.id

在下面的代码中,我只想在GROUPBY条件中给出的三列上应用group函数。但它给出了错误的、错误数量的论点

select a.id,a.name,  
       c.code,
       a.active_dt,
       a.inactive_dt,
       b.category,
       count(a.id,a.name,c.code) count
from student a,class b, descrip c
where a.id=b.id
and a.id=c.id
group by a.id,a.name,c.code
    having count(a.id,a.name,c.code) >1;
你是说像这样吗

SELECT * FROM (
    SELECT
        a.id,
        a.name,
        c.code,
        a.active_dt,
        a.inactive_dt,
        b.category,
        COUNT(1) OVER(PARTITION BY a.id, a.name, c.code) AS CNT
    FROM
        student a,
        class b,
        descrip c
    WHERE
        a.id=b.id AND
        a.id=c.id
)
WHERE CNT > 1
试试这个:

SELECT a.id, a.name, c.code, a.active_dt, a.inactive_dt, b.category, 
       count(DISTINCT a.id,a.name,c.code) [count]
FROM student a
INNER JOIN class b
    ON a.id=b.id
INNER JOIN descrip c
    ON a.id=c.id
GROUP BY a.id, a.name, c.code, a.active_dt, a.inactive_dt, b.category
HAVING count(a.id,a.name,c.code) > 1

如果我理解得很好,您只需要:

select a.id,a.name,  
       c.code,
       a.active_dt,
       a.inactive_dt,
       b.category,
       count(*) count
from student a,class b, descrip c
where a.id=b.id
and a.id=c.id
group by a.id,a.name,c.code
    having count(*) >1;
count()
ad
group by
工作原理的一个小示例:

create table testGroup(a, b, c) as ( 
select 1, 2, 3 from dual union all
select 1, 2, 3 from dual union all
select 5, 6, 7 from dual union all
select 5, 6, 7 from dual union all
select 5, 6, 9 from dual union all
select 1, 2, 9 from dual 
)
您需要在组中使用的列数不影响您使用计数的方式;假设您需要按3列进行分组:

SQL> select a, b, c, count(*)
  2  from testGroup
  3  group by a, b, c
  4  having count(*) > 1;

         A          B          C   COUNT(*)
---------- ---------- ---------- ----------
         5          6          7          2
         1          2          3          2
如果只需要按2列进行聚合:

SQL> select a, b, count(*)
  2  from testGroup
  3  group by a, b
  4  having count(*) > 1;

         A          B   COUNT(*)
---------- ---------- ----------
         5          6          3
         1          2          3

这假设您需要对行进行计数,不管值是否为null。

此代码包含许多错误。添加您想要执行的操作,以及示例数据和预期输出
count(a.id,a.name,c.code)
这是一个看起来很奇怪的查询
count(a.id,a.name,c.code)
应该做什么?你需要计算所有的行,或者只计算至少一个
a.id,a.name,c.code
不为空的行吗?@Aleksej我只希望数据根据3个冒号进行分组,即a.id,a.name,c.code。那么,为什么不
count(*)
?对Oracle不是很确定。但是它在sql中可以工作@Aleksejb即使这在某些DBMS中有效,在Oracle中也不起作用,问题是Oracle。是的,你是对的,但在你提到的上一个表中,我想选择a、b、c,即所有列。我不明白这一点。根据我的示例表,结果应该是什么?