oracle Grouping_sets在分组依据更改时获取不同的输出格式

lvjbypge  于 2023-01-25  发布在  Oracle
关注(0)|答案(1)|浏览(105)

我有下面的测试用例,运行良好。
当我尝试按customer_id,TO_CHAR(p. purchase_date,'IYYY "W" IW')分组时,我得到的格式与工作查询的格式不同。作为一种解决方案,我可以使用where子句将查询限制为每次1个customer_id,但我真的不想这样做。
有没有人能告诉我问题是什么,以及如何纠正这个问题。

ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'DD-MON-YYYY  HH24:MI:SS.FF';

ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY HH24:MI:SS';
CREATE TABLE customers 
(CUSTOMER_ID, FIRST_NAME, LAST_NAME) AS
SELECT 1, 'Faith', 'Mazzarone' FROM DUAL UNION ALL
SELECT 2, 'Lisa', 'Saladino' FROM DUAL UNION ALL
SELECT 3, 'Micheal', 'Palmice' FROM DUAL UNION ALL
SELECT 4, 'Jerry', 'Torchiano' FROM DUAL;

CREATE TABLE items 
(PRODUCT_ID, PRODUCT_NAME, PRICE) AS
SELECT 100, 'Black Shoes', 79.99 FROM DUAL UNION ALL
SELECT 101, 'Brown Pants', 111.99 FROM DUAL UNION ALL
SELECT 102, 'White Shirt', 10.99 FROM DUAL;

CREATE TABLE purchases(
    PURCHASE_ID NUMBER GENERATED BY DEFAULT AS IDENTITY (START WITH 1) NOT NULL,
    CUSTOMER_ID NUMBER, 
    PRODUCT_ID NUMBER, 
    QUANTITY NUMBER, 
   PURCHASE_DATE TIMESTAMP
);
INSERT  INTO purchases
(CUSTOMER_ID, PRODUCT_ID, QUANTITY, PURCHASE_DATE) 
SELECT 1, 101, 3, TIMESTAMP'2022-10-11 09:54:48' FROM DUAL UNION ALL
SELECT 1, 100, 1, TIMESTAMP '2022-10-12 19:04:18' FROM DUAL UNION ALL
SELECT 2, 101,1, TIMESTAMP '2022-10-11 09:54:48' FROM DUAL UNION ALL
SELECT 2, 101, 3, TIMESTAMP '2022-10-17 19:34:58' FROM DUAL UNION ALL
SELECT 2, 102, 3,TIMESTAMP '2022-12-06 11:41:25' + NUMTODSINTERVAL ( LEVEL * 2, 'DAY') FROM  dual CONNECT BY  LEVEL <= 6 UNION ALL
SELECT 2, 102, 3,TIMESTAMP '2022-12-26 11:41:25' + NUMTODSINTERVAL ( LEVEL * 2, 'DAY') FROM  dual CONNECT BY  LEVEL <= 6 UNION ALL
SELECT 3, 101,1, TIMESTAMP '2022-12-21 09:54:48' FROM DUAL UNION ALL
SELECT 3, 102,1, TIMESTAMP '2022-12-27 19:04:18' FROM DUAL UNION ALL
SELECT 3, 102, 4,TIMESTAMP '2022-12-22 21:44:35' + NUMTODSINTERVAL ( LEVEL * 2, 'DAY') FROM    dual
CONNECT BY  LEVEL <= 15 UNION ALL 
SELECT 3, 101,1, TIMESTAMP '2022-12-11 09:54:48' FROM DUAL UNION ALL
SELECT 3, 102,1, TIMESTAMP '2022-12-17 19:04:18' FROM DUAL UNION ALL
SELECT 3, 102, 4,TIMESTAMP '2022-12-12 21:44:35' + NUMTODSINTERVAL ( LEVEL * 2, 'DAY') FROM    dual
CONNECT BY  LEVEL <= 5;
ALTER TABLE customers 
ADD CONSTRAINT customers_pk PRIMARY KEY (customer_id);

ALTER TABLE items 
ADD CONSTRAINT items_pk PRIMARY KEY (product_id);

ALTER TABLE purchases 
ADD CONSTRAINT purchases_pk PRIMARY KEY (purchase_id);

ALTER TABLE purchases ADD CONSTRAINT customers_fk FOREIGN KEY (customer_id) REFERENCES customers(customer_id);

ALTER TABLE purchases ADD CONSTRAINT items_fk FOREIGN KEY (PRODUCT_ID) REFERENCES items(product_id);
/* works fine */

SELECT    TO_CHAR (p.purchase_date, 'IYYY"W"IW')    AS year_week
,     p.customer_id
,     c.first_name
,     c.last_name
,     SUM (p.quantity * i.price)        AS total_amt
FROM      purchases  p
JOIN      customers  c  ON  p.customer_id = c.customer_id
JOIN      items      i  ON  p.product_id  = i.product_id
GROUP BY  GROUPING SETS (   (TO_CHAR (p.purchase_date, 'IYYY"W"IW'), p.customer_id, c.first_name, c.last_name)
      , (TO_CHAR (p.purchase_date, 'IYYY"W"IW'))
, ()
)
ORDER BY  TO_CHAR (p.purchase_date, 'IYYY"W"IW'), p.customer_id;

| 年_周|客户ID|名字|姓氏|总计|
| - ------|- ------|- ------|- ------|- ------|
| 小行星2022|1个|信仰|马扎罗内|四百一十五点九六|
| 小行星2022|第二章|丽莎|萨拉迪诺|一百一十一点九十九分|
| 小行星2022|- -|- -|- -|五百二十七块九五|
| 小行星2022|第二章|丽莎|萨拉迪诺|三百三十五块九七|
| 小行星2022|- -|- -|- -|三百三十五块九七|
| 小行星2022|第二章|丽莎|萨拉迪诺|六十五点九四分|
| 小行星2022|三个|米凯尔|帕尔米采|一百一十一点九十九分|
| 小行星2022|- -|- -|- -|一百七十七点九三|
| 小行星2022|第二章|丽莎|萨拉迪诺|一百三十一点八八|
| 小行星2022|三个|米凯尔|帕尔米采|一百四十二点八七|
| 小行星2022|- -|- -|- -|二百七十四点七五分|
| 小行星2022|三个|米凯尔|帕尔米采|二百四十三点八七|
| 小行星2022|- -|- -|- -|二百四十三点八七|
| 小行星2022|第二章|丽莎|萨拉迪诺|九十八点九一分|
| 小行星2022|三个|米凯尔|帕尔米采|一百八十六点八三|
| 小行星2022|- -|- -|- -|二百八十五点七四|
| 小行星2023|第二章|丽莎|萨拉迪诺|九十八点九一分|
| 小行星2023|三个|米凯尔|帕尔米采|一百三十一点八八|
| 小行星2023|- -|- -|- -|二百三十点七九|
| 小行星2023|三个|米凯尔|帕尔米采|一百七十五点八四|
| 小行星2023|- -|- -|- -|一百七十五点八四|
| 小行星2023|三个|米凯尔|帕尔米采|一百三十一点八八|
| 小行星2023|- -|- -|- -|一百三十一点八八|
| - |- -|- -|- -|二千三百八十四点七二|

/* unexpected output */

SELECT        p.customer_id,
      c.first_name,
      c.last_name,
TO_CHAR (p.purchase_date, 'IYYY"W"IW') AS year_week,
      SUM (p.quantity * i.price)        AS total_amt
FROM      purchases  p
JOIN      customers  c  ON  p.customer_id = c.customer_id
JOIN      items      i  ON  p.product_id  = i.product_id
GROUP BY  GROUPING SETS ( 
    (p.customer_id, c.first_name, c.last_name),
    (TO_CHAR (p.purchase_date, 'IYYY"W"IW')), 
(p.customer_id, c.first_name, c.last_name)
, ()
)
ORDER BY  p.customer_id,
TO_CHAR (p.purchase_date, 'IYYY"W"IW');
8yparm6h

8yparm6h1#

SELECT        p.customer_id,
      c.first_name,
      c.last_name,
TO_CHAR (p.purchase_date, 'IYYY"W"IW') AS year_week,
      SUM (p.quantity * i.price)        AS total_amt
FROM      purchases  p
JOIN      customers  c  ON  p.customer_id = c.customer_id
JOIN      items      i  ON  p.product_id  = i.product_id
GROUP BY  GROUPING SETS ( 
    (p.customer_id, c.first_name, c.last_name,  (TO_CHAR (p.purchase_date, 'IYYY"W"IW')))
, p.customer_id 
, ()
)
ORDER BY  p.customer_id,
TO_CHAR (p.purchase_date, 'IYYY"W"IW');

CUSTOMER_ID FIRST_NAME  LAST_NAME   YEAR_WEEK   TOTAL_AMT
1   Faith   Mazzarone   2022W41 415.96
1    -   -   -  415.96
2   Lisa    Saladino    2022W41 111.99
2   Lisa    Saladino    2022W42 335.97
2   Lisa    Saladino    2022W49 65.94
2   Lisa    Saladino    2022W50 131.88
2   Lisa    Saladino    2022W52 98.91
2   Lisa    Saladino    2023W01 98.91
2    -   -   -  843.6
3   Micheal Palmice 2022W49 111.99
3   Micheal Palmice 2022W50 142.87
3   Micheal Palmice 2022W51 243.87
3   Micheal Palmice 2022W52 186.83
3   Micheal Palmice 2023W01 131.88
3   Micheal Palmice 2023W02 175.84
3   Micheal Palmice 2023W03 131.88
3    -   -   -  1125.16
 -   -   -   -  2384.72

相关问题