oracle 如何让第一个单词重复在前面,然后在列表中休息的单词?

qoefvg9y  于 2023-02-03  发布在  Oracle
关注(0)|答案(1)|浏览(99)

我不知道如何获得正确的脚本
通常,我使用listagg by order by,但我想要一些不同的东西
选择
客户标识,
将组(按汽车_产品排序)内的(汽车_产品,'-')作为产品列出
购自
按客户标识分组
我想输出的结果,如保持第一锌,然后其余的是listagg,这里是一个例子;
| 客户标识|积|
| - ------|- ------|
| 一百二十三|锌蓝红|
| 九三九|黑-蓝|
| 五六七|锌-黑-红-黄|
是的939-没有锌,所以只要选择其他在那里
谢谢

bnlyeluc

bnlyeluc1#

修复order by子句,使其先对ZINC排序,然后对其余的排序。
样本数据:

SQL> with purchase (customer_id, car_product) as
  2    (select 123, 'ZINC'  from dual union all
  3     select 123, 'BLUE'  from dual union all
  4     select 123, 'RED'   from dual union all
  5     --
  6     select 939, 'BLACK' from dual union all
  7     select 939, 'BLUE'  from dual
  8    )

质询:

9  select customer_id,
 10    listagg(car_product, '-') within group
 11      (order by case when car_product = 'ZINC' then 'A' else car_product end) as product
 12  from purchase
 13  group by customer_id;

CUSTOMER_ID PRODUCT
----------- --------------------
        123 ZINC-BLUE-RED
        939 BLACK-BLUE

SQL>

相关问题