缩减列不适用于 oracle 中的表达式分组

Reduction columns are not working for group by expression in oracle

我尝试连接两列并使用 group by expression,但它不起作用。如何在 oracle 中对多个列进行分组。 first_name和last_name是缩减数据。

SELECT employee_id,
  employee_name,
  employee_unique
FROM
  (SELECT a.id AS employee_id,
    (a.first_name
    || a.last_name) AS employee_name,
    b.employee_unique
  FROM A a
  INNER JOIN b
  ON a.id=b.employee_id
  GROUP BY a.id,
    b.employee_unique,
    (a.first_name
    || a.last_name)
  );

"not work" 是什么意思?它对我有用(虽然,因为我没有你的表,我使用了 Scott 的 EMPDEPT,但其他的都差不多)。如果不正确,您应该解释 哪里有问题。如果您希望我们使用您的数据,请提供 CREATE TABLEINSERT INTO 样本数据。

SQL> select employee_id,
  2         employee_name,
  3         employee_unique
  4  from
  5    (select a.empno          as employee_id,
  6            a.ename || a.job as employee_name,
  7            b.dname          as employee_unique
  8    from emp a
  9    inner join dept b
 10       on a.deptno=b.deptno
 11    group by a.empno,
 12             b.dname,
 13             a.ename || a.job
 14    );

EMPLOYEE_ID EMPLOYEE_NAME       EMPLOYEE_UNIQU
----------- ------------------- --------------
       7654 MARTINSALESMAN      SALES
       7876 ADAMSCLERK          RESEARCH
       7566 JONESMANAGER        RESEARCH
       7698 BLAKEMANAGER        SALES
       7844 TURNERSALESMAN      SALES
       7369 SMITHCLERK          RESEARCH
       7788 SCOTTANALYST        RESEARCH
       7900 JAMESCLERK          SALES
       7902 FORDANALYST         RESEARCH
       7782 CLARKMANAGER        ACCOUNTING
       7934 MILLERCLERK         ACCOUNTING
       7499 ALLENSALESMAN       SALES
       7521 WARDSALESMAN        SALES
       7839 KINGPRESIDENT       ACCOUNTING

14 rows selected.

SQL>

不过,因为没有真正要分组的东西(这里没有聚合),您可以使用 distinct(没有 group by 子句)并得到相同的结果:

select employee_id,
       employee_name,
       employee_unique
from
  (select distinct
          a.empno          as employee_id,
          a.ename || a.job as employee_name,
          b.dname          as employee_unique
  from emp a
  inner join dept b
     on a.deptno=b.deptno
  );