saraa
saraa

Reputation: 51

Oracle query -ListAgg

I need to write query for below case in Oracle(Actually this is a sample table) This is DB Table structure

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. (Here Type 1 - Created date and Type 2 is Transaction date )

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.)

Thanks in advance

Upvotes: 1

Views: 62

Answers (1)

Littlefoot
Littlefoot

Reputation: 142705

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>

Upvotes: 1

Related Questions