Oracle LISTAGG按日期和位置的合计计数

6fe3ivhb  于 2023-05-22  发布在  Oracle
关注(0)|答案(1)|浏览(168)

我在Windows上运行Oracle 12.1。我需要帮助创建一个列表聚合的日期(全天是一天的条件,trunc(日期))和地点,并计算清单结果中的库存类型的金额。我计划将其作为car_equipment表的视图来显示列表聚合。
在显示max(date)的库存列表之前,我做了一个简单的视图。

CREATE OR REPLACE FORCE NONEDITIONABLE VIEW "DB1"."CAR_INVENTORY_VW" ("MAX_INVENTORY_DATE","LOCATION", "INVENTORY_LIST") AS 
 SELECT 
  inventory_date max_inventory_date,
  location,
  LISTAGG(num_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;

但现在我想显示一个地点的所有日期,并按日期收集每个地点的库存类型计数的总和。下面是创建表和行的代码:首先创建表并插入数据条件示例行。

CREATE TABLE "CAR_EQUIPMENT" 
   ("NUM_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" (NUM_CARS, EQUIPMENT_TYPE, LOCATION, INVENTORY_DATE) VALUES ('8', 'Rovers', 'coventry', TO_DATE('2019-09-07 09:00:00', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO "CAR_EQUIPMENT" (NUM_CARS, EQUIPMENT_TYPE, LOCATION, INVENTORY_DATE) VALUES ('1', 'Rovers', 'coventry', TO_DATE('2019-09-07 18:00:00', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO "CAR_EQUIPMENT" (NUM_CARS, EQUIPMENT_TYPE, LOCATION, INVENTORY_DATE) VALUES ('9', 'Jaguars', 'coventry', TO_DATE('2019-09-07 06:00:00', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO "CAR_EQUIPMENT" (NUM_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" (NUM_CARS, EQUIPMENT_TYPE, LOCATION, INVENTORY_DATE) VALUES ('10','Trans Am', 'leamington', TO_DATE('2019-08-30 09:00:00', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO "CAR_EQUIPMENT" (NUM_CARS, EQUIPMENT_TYPE, LOCATION, INVENTORY_DATE) VALUES ('2','Trans Am', 'leamington', TO_DATE('2019-08-30 18:00:00', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO "CAR_EQUIPMENT" (NUM_CARS, EQUIPMENT_TYPE, LOCATION, INVENTORY_DATE) VALUES ('8', 'Rovers', 'coventry', TO_DATE('2019-09-06 18:00:00', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO "CAR_EQUIPMENT" (NUM_CARS, EQUIPMENT_TYPE, LOCATION, INVENTORY_DATE) VALUES ('4', 'Rovers', 'leamington', TO_DATE('2019-09-06 09:00:00', 'YYYY-MM-DD HH24:MI:SS'));

commit;

我找不到可以在listagg中获得sum(num_汽车)的示例。
另外,(我不想在这里问一个后续问题),但是如果列表聚合超过4000字节会发生什么?我读到可以使用xmlagg,但是如果结果超过4000字节,显示列表的最佳方式是什么呢?你以前遇到过这个问题吗?你是如何解决这个问题的?再次感谢您的任何建议。最亲切的问候,吉利
我试过:

create or replace view TEST_LAGG(
  WITH
  TEMP AS (SELECT LOCATION,
                  trunc(INVENTORY_DATE) inventory_date,
                  EQUIPMENT_TYPE,
                  sum(NUM_CARS) sum_num_cars
           from CAR_EQUIPMENT
           group by LOCATION,
                    trunc(INVENTORY_DATE),
                    EQUIPMENT_TYPE
         )
   select LOCATION,
          INVENTORY_DATE,
          listagg(sum_num_cars ||' '|| EQUIPMENT_TYPE, ', ') within group (order by inventory_date) lagg
   from TEMP
   group by LOCATION,
        inventory_date);

但我得到ORA-00903:无效表名00903。00000 -“无效的表名”
再次感谢Littlefoot,我尝试了:

create or replace view TEST_LAGG
 AS WITH
  TEMP (SELECT LOCATION,
                  trunc(INVENTORY_DATE) inventory_date,
                  EQUIPMENT_TYPE,
                  sum(NUM_CARS) sum_num_cars
           from CAR_EQUIPMENT
           group by LOCATION,
                   trunc(INVENTORY_DATE),
                    EQUIPMENT_TYPE
         )
    select LOCATION,
      INVENTORY_DATE,
      listagg(sum_num_cars ||' '|| EQUIPMENT_TYPE, ', ') within group   (order by inventory_date) lagg
 from TEMP
 group by LOCATION,
        inventory_date;

但我得到了错误:ORA-00903:无效的表名00903。00000 -“无效的表名”* 原因:

  • 操作:行出错:135柱:3 -TEMP表参考线。
8qgya5xd

8qgya5xd1#

首先必须计算出这个和,然后在listagg中使用它。就像这样:

SQL>     with
  2        temp as (select location, inventory_date, equipment_type, sum(num_cars) sum_num_cars
  3                 from car_equipment
  4                 group by location, inventory_date, equipment_type
  5                )
  6      select location,
  7             inventory_date,
  8             listagg(sum_num_cars ||' '|| equipment_type, ', ') within group (order by inventory_date) lagg
  9      from temp
 10      group by location,
 11               inventory_date;

LOCATION        INVENTORY_DATE      LAGG
--------------- ------------------- ------------------------------
coventry        2019-09-06 18:00:00 8 Rovers
coventry        2019-09-07 06:00:00 9 Jaguars
coventry        2019-09-07 09:00:00 8 Rovers
coventry        2019-09-07 18:00:00 1 Rovers
leamington      2019-08-30 00:00:00 7 Rovers
leamington      2019-08-30 09:00:00 10 Trans Am
leamington      2019-08-30 18:00:00 2 Trans Am
leamington      2019-09-06 09:00:00 4 Rovers

8 rows selected.

SQL>

现在,这取决于你想得到什么样的结果。
关于你的第二个问题:是的,如果结果超过了4000个字符的限制,你就必须使用XMLAGG。

编辑

哈哈,看来你需要的是TRUNC(inventory_date)。它将从日期列中删除时间组件。

SQL> with
  2    temp as (select location,
  3                    trunc(inventory_date) inventory_date,
  4                    equipment_type,
  5                    sum(num_cars) sum_num_cars
  6             from car_equipment
  7             group by location,
  8                      trunc(inventory_date),
  9                      equipment_type
 10            )
 11  select location,
 12         inventory_date,
 13         listagg(sum_num_cars ||' '|| equipment_type, ', ') within group (order by inventory_date) lagg
 14  from temp
 15  group by location,
 16           inventory_date;

LOCATION        INVENTORY_DATE      LAGG
--------------- ------------------- ------------------------------
coventry        2019-09-06 00:00:00 8 Rovers
coventry        2019-09-07 00:00:00 9 Jaguars, 9 Rovers
leamington      2019-08-30 00:00:00 12 Trans Am, 7 Rovers
leamington      2019-09-06 00:00:00 4 Rovers

SQL>

编辑:创建视图

SQL> create or replace view TEST_LAGG as
  2    WITH
  3    TEMP AS (SELECT LOCATION,
  4                    trunc(INVENTORY_DATE) inventory_date,
  5                    EQUIPMENT_TYPE,
  6                    sum(NUM_CARS) sum_num_cars
  7             from CAR_EQUIPMENT
  8             group by LOCATION,
  9                      trunc(INVENTORY_DATE),
 10                      EQUIPMENT_TYPE
 11           )
 12     select LOCATION,
 13            INVENTORY_DATE,
 14            listagg(sum_num_cars ||' '|| EQUIPMENT_TYPE, ', ') within group (order by inventory_date) lagg
 15     from TEMP
 16     group by LOCATION,
 17          inventory_date;

View created.

SQL> select * from test_lagg;

LOCATION        INVENTORY_DATE      LAGG
--------------- ------------------- ------------------------------
coventry        2019-09-06 00:00:00 8 Rovers
coventry        2019-09-07 00:00:00 9 Jaguars, 9 Rovers
leamington      2019-08-30 00:00:00 12 Trans Am, 7 Rovers
leamington      2019-09-06 00:00:00 4 Rovers

SQL>

相关问题