简体   繁体   English

使用 listagg 时不按表达式分组 function

[英]Getting not a group by expression while using listagg function

CREATE TABLE source_det (
    e_id       NUMBER(10),
    sys_name   VARCHAR2(20),
    ref_id     NUMBER(10),
    sys_other  VARCHAR2(30)
);

INSERT INTO source_det VALUES(11,'SOURCE',992,null);
INSERT INTO source_det VALUES(11,'SOURCE',637,null);
INSERT INTO source_det VALUES(11,'SOURCE',null,'Manual');
INSERT INTO source_det VALUES(11,'TARGET',637,null);

commit;

My Attempt:我的尝试:

SELECT e_id,
       LISTAGG(source, ';') source,
       LISTAGG(target, ';') target,
       source_other
  FROM (SELECT e_id,
               CASE
                 WHEN sys_name = 'SOURCE' THEN
                  ref_id
               END source,
               CASE
                 WHEN sys_name = 'TARGET' THEN
                  ref_id
               END target,
               CASE
                 WHEN sys_name = 'SOURCE' AND ref_id IS NULL THEN
                  sys_other
               END source_other
          FROM source_det
         GROUP BY e_id);

From the above set of data I need only a single row to be returned from the SELECT query but I am getting null values instead along with the required values.从上面的数据集中,我只需要从SELECT查询返回一行,但我得到的是null值以及所需的值。 If sys_name is SOURCE then the result should be ref_id .如果sys_name是 SOURCE 那么结果应该是ref_id If there is more than one record then it should be spilitted by delimiter;如果有多个记录,则应使用定界符将其拆分; If sys_name is TARGET then result should be ref_id If sys_name is SOURCE AND ref_id is null then it should give sys_other as a result.如果sys_name是 TARGET 那么结果应该是ref_id如果sys_name是 SOURCE AND ref_id是 null 那么它应该给sys_other作为结果。 But from my query I am not getting a single row instead getting 4 rows.但是从我的查询中我没有得到一行而是得到 4 行。 But ideally, I need only one row with e_id 11 And most important is in place of null values it could be possible that there would be () present in place of null. So, need to exclude this as well.但理想情况下,我只需要一行e_id 11 最重要的是代替 null 值,可能会有()代替 null。因此,也需要排除这一点。 After getting the desired result then we can use DECODE(ref_id,'()',null) like this.获得所需结果后,我们可以像这样使用 DECODE(ref_id,'()',null) 。

Expected result:预期结果:

e_id e_id source来源 target目标 source_other来源_其他
11 11 992;637 992;637 637 637 Manual手动的

Tool used: SQL Developer (18c)使用的工具:SQL Developer (18c)

You don't need a subquery, just use like the following one below您不需要子查询,只需像下面这样使用

SELECT e_id,
       LISTAGG(CASE
                 WHEN sys_name = 'SOURCE' THEN
                  ref_id
               END,
               ';') WITHIN GROUP(ORDER BY ref_id DESC) AS source,
       LISTAGG(CASE
                 WHEN sys_name = 'TARGET' THEN
                  ref_id
               END,
               ';') WITHIN GROUP(ORDER BY ref_id DESC) AS target,
       LISTAGG(CASE
                 WHEN sys_name = 'SOURCE' AND ref_id IS NULL THEN
                  DECODE(sys_other,'()','',sys_other)
               END,
               ';') WITHIN GROUP(ORDER BY 0) AS source_other
  FROM source_det
 GROUP BY e_id

where missing WITHIN GROUP (ORDER BY..) should follow LISTAGG(...) expression其中缺少WITHIN GROUP (ORDER BY..)应该遵循LISTAGG(...)表达式

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM