How to derive max date on LISTAGG Oracle 12c

Gilly

Oracle 12c installation. I am having trouble organizing Oracle listagg() to show the count of cars column of my query to display the results of only the max(inventory_date) data per location.

I was thinking to create a view to aggregate the results:

create or replace view car_inventory_vw as 
select max(a.inventory_date) as max_date,
a.location,LISTAGG(a.COUNT_CARS||' '||a.EQUIPMENT_TYPE,',')
within GROUP (ORDER BY a.equipment_type) as equipment_list 
from car_equipment a 
group by a.location;

Create the table and insert test rows, Installed on Oracle 12c on Windows

CREATE TABLE "CAR_EQUIPMENT" 
   (    "COUNT_CARS" NUMBER(10,0), 
"EQUIPMENT_TYPE" VARCHAR2(100 BYTE), 
"LOCATION" VARCHAR2(500 BYTE), 
"INVENTORY_DATE" DATE) 
SEGMENT CREATION IMMEDIATE 
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 
NOCOMPRESS LOGGING
   STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
   PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
   BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
   TABLESPACE "USERS" ;

INSERT INTO "CAR_EQUIPMENT" (COUNT_CARS, EQUIPMENT_TYPE, LOCATION,     INVENTORY_DATE) VALUES ('8', 'Rovers', 'coventry', TO_DATE('2019-09-07 00:00:00', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO "CAR_EQUIPMENT" (COUNT_CARS, EQUIPMENT_TYPE, LOCATION, INVENTORY_DATE) VALUES ('9', 'Jaguars', 'coventry', TO_DATE('2019-09-07 00:00:00', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO "CAR_EQUIPMENT" (COUNT_CARS, EQUIPMENT_TYPE, LOCATION, INVENTORY_DATE) VALUES ('7', 'Rovers', 'leamington', TO_DATE('2019-08-30 00:00:00', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO "CAR_EQUIPMENT" (COUNT_CARS, EQUIPMENT_TYPE, LOCATION, INVENTORY_DATE) VALUES ('10','Trans Am', 'leamington', TO_DATE('2019-08-30 00:00:00', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO "CAR_EQUIPMENT" (COUNT_CARS, EQUIPMENT_TYPE, LOCATION, INVENTORY_DATE) VALUES ('8', 'Rovers', 'coventry', TO_DATE('2019-01-07 00:00:00', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO "CAR_EQUIPMENT" (COUNT_CARS, EQUIPMENT_TYPE, LOCATION, INVENTORY_DATE) VALUES ('4', 'Rovers', 'leamington', TO_DATE('2019-01-08 00:00:00', 'YYYY-MM-DD HH24:MI:SS'));

Results should be the inventory list of the max(inventory_date) by location:

MAX(INVENTORY_DATE) LOCATION    INVENTORY_LIST
-------------------- ----------- -------------------------
2019-08-30          leamington  10 Trans AM, 7 Rovers
2019-09-07          coventry    8 Rovers, 9 Jaguars  

I am stuck on a simple problem, please advise how I can get the list created for the max date data only by location.

Thank you in advance, Gilly

GMB

You could proceed in two steps :

  • first, use RANK() in a subquery to identify the records that correspond to the latest inventory date at each location

  • then, use aggregation in the outer query and generate the inventory list of each location using LISTAGG()

Query:

SELECT 
    inventory_date max_inventory_date,
    location,
    LISTAGG(count_cars || ' ' || equipment_type, ', ') 
        WITHIN GROUP (ORDER BY equipment_type) inventory_list
FROM (
    SELECT c.*, RANK() OVER(PARTITION BY location ORDER BY inventory_date DESC) rn
    FROM car_equipment c
) x 
WHERE rn = 1
GROUP BY inventory_date, location;

Demo on DB Fiddle

Data:

COUNT_CARS | EQUIPMENT_TYPE | LOCATION   | INVENTORY_DATE
---------: | :------------- | :--------- | :-------------
         8 | Rovers         | coventry   | 07-SEP-19     
         9 | Jaguars        | coventry   | 07-SEP-19     
         7 | Rovers         | leamington | 30-AUG-19     
        10 | Trans Am       | leamington | 30-AUG-19     
         8 | Rovers         | coventry   | 07-JAN-19     
         4 | Rovers         | leamington | 08-JAN-19     

Results:

MAX_INVENTORY_DATE | LOCATION   | INVENTORY_LIST       
:----------------- | :--------- | :--------------------
30-AUG-19          | leamington | 7 Rovers, 10 Trans Am
07-SEP-19          | coventry   | 9 Jaguars, 8 Rovers  

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Retrieve distinct values with LISTAGG in Oracle 12C

分類Dev

How to change SYS and SYSTEM password in oracle 12c?

分類Dev

Oracle:Listagg

分類Dev

Getting date time stamp difference minutes in Oracle Database 12c Enterprise Edition

分類Dev

How to create partitioned table based on one Integer column (each value = 1 partition) in Oracle 12c?

分類Dev

How to connect to Azure Oracle 12c Database using sqlplus or sql developer cloud connection

分類Dev

Oracle Listagg Sub query

分類Dev

Oracle 12cの設定?

分類Dev

cx_oracle 6 for Oracle 12C version error

分類Dev

Oracle 12c:trunc(date)のバグにより、誤ったデータが生成される

分類Dev

Oracle - Max value from each date with date/time

分類Dev

Oracle12CのLISTAGGを使用して個別の値を取得する

分類Dev

Oracle 12c: Multiple functions in a SELECT statement's WITH clause

分類Dev

Oracle 12c RAC GV $ SESSIONEVENT列

分類Dev

Oracle10gのLISTAGG代替

分類Dev

listagg 問題の Oracle RTRIM

分類Dev

How to derive from vector class

分類Dev

C#Encoding.ASCII.GetBytesと同等のOracle 12c

分類Dev

@Temporal(TemporalType.DATE)とOracle 12

分類Dev

PreparedStatement + Select for update + Oracle 12c + ORA-01461 in primary key column

分類Dev

Tomcat 9 w / Apache DBCP + Spring 5 + Oracle 12c + SqlArrayValue

分類Dev

ハウツー:Grails 3.0.2 + Oracle Database 12c?

分類Dev

ORA-01749-Oracle 12c vs 11g

分類Dev

Downloading Oracle database 12c Release 1 (12.1.0.2.0) on Linux via wget

分類Dev

Geometries using a user defined coordinate reference system do not validate in Oracle 12c

分類Dev

ORACLE FORMS6Iから12Cへの移行

分類Dev

Oracle 12C SQLAggregateクエリロジック

分類Dev

Cannot connect to Oracle 12c from Tomcat 6: no matching authentication protocol (ORA-28040)

分類Dev

Oracle 12cを使用したJmeter:「;」使用できません

Related 関連記事

  1. 1

    Retrieve distinct values with LISTAGG in Oracle 12C

  2. 2

    How to change SYS and SYSTEM password in oracle 12c?

  3. 3

    Oracle:Listagg

  4. 4

    Getting date time stamp difference minutes in Oracle Database 12c Enterprise Edition

  5. 5

    How to create partitioned table based on one Integer column (each value = 1 partition) in Oracle 12c?

  6. 6

    How to connect to Azure Oracle 12c Database using sqlplus or sql developer cloud connection

  7. 7

    Oracle Listagg Sub query

  8. 8

    Oracle 12cの設定?

  9. 9

    cx_oracle 6 for Oracle 12C version error

  10. 10

    Oracle 12c:trunc(date)のバグにより、誤ったデータが生成される

  11. 11

    Oracle - Max value from each date with date/time

  12. 12

    Oracle12CのLISTAGGを使用して個別の値を取得する

  13. 13

    Oracle 12c: Multiple functions in a SELECT statement's WITH clause

  14. 14

    Oracle 12c RAC GV $ SESSIONEVENT列

  15. 15

    Oracle10gのLISTAGG代替

  16. 16

    listagg 問題の Oracle RTRIM

  17. 17

    How to derive from vector class

  18. 18

    C#Encoding.ASCII.GetBytesと同等のOracle 12c

  19. 19

    @Temporal(TemporalType.DATE)とOracle 12

  20. 20

    PreparedStatement + Select for update + Oracle 12c + ORA-01461 in primary key column

  21. 21

    Tomcat 9 w / Apache DBCP + Spring 5 + Oracle 12c + SqlArrayValue

  22. 22

    ハウツー:Grails 3.0.2 + Oracle Database 12c?

  23. 23

    ORA-01749-Oracle 12c vs 11g

  24. 24

    Downloading Oracle database 12c Release 1 (12.1.0.2.0) on Linux via wget

  25. 25

    Geometries using a user defined coordinate reference system do not validate in Oracle 12c

  26. 26

    ORACLE FORMS6Iから12Cへの移行

  27. 27

    Oracle 12C SQLAggregateクエリロジック

  28. 28

    Cannot connect to Oracle 12c from Tomcat 6: no matching authentication protocol (ORA-28040)

  29. 29

    Oracle 12cを使用したJmeter:「;」使用できません

ホットタグ

アーカイブ