[英]Oracle query -ListAgg
I need to write query for below case in Oracle(Actually this is a sample table) This is DB Table structure我需要在 Oracle 中编写以下案例的查询(实际上这是一个示例表)这是 DB 表结构
if the table contains two set of dates for selected Item_id, then based on type one will be created date,other one is transaction date.如果该表包含选定 Item_id 的两组日期,则基于类型将创建日期,另一组是交易日期。 (Here Type 1 - Created date and Type 2 is Transaction date )
(这里类型 1 - 创建日期和类型 2 是交易日期)
My final out come will be as below : Output我的最终结果如下:输出
Please any one help me to write query for this case in oracle ( I'm new to oracle. As I know we should use "ListAgg" to achieve this output.)请任何人帮助我在 oracle 中为这种情况编写查询(我是 oracle 的新手。据我所知,我们应该使用“ListAgg”来实现此输出。)
Thanks in advance提前致谢
As far as I can tell, there's nothing to listagg ;据我所知,没有什么可列出的; ordinary aggregates will do the job.
普通的聚合将完成这项工作。
SQL> alter session set nls_date_format = 'dd/mm/yyyy';
Session altered.
SQL> with test (cat_id, type, cdate, item_id) as
2 (select 1, 1, date '2019-04-09', 46 from dual union all
3 select 2, 1, date '2019-03-05', 47 from dual union all
4 select 3, 2, date '2019-04-10', 46 from dual union all
5 select 4, 2, date '2019-04-06', 52 from dual
6 )
7 select item_id,
8 min(case when type = 1 then cdate end) created_date,
9 max(case when type = 2 then cdate end) transaction_date
10 from test
11 group by item_id
12 order by item_id;
ITEM_ID CREATED_DA TRANSACTIO
---------- ---------- ----------
46 09/04/2019 10/04/2019
47 05/03/2019
52 06/04/2019
SQL>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.