我尝试使用以下查询列出在过去30天内未进行过购买的所有客户:
SELECT c.*
FROM customers c
WHERE NOT EXISTS (
SELECT 1
FROM purchases p
WHERE c.customer_id = p.customer_id
AND p.PURCHASE_DATE >= TRUNC(SYSTIMESTAMP) - NUMTODSINTERVAL (30, 'DAY')
AND p.PURCHASE_DATE < TRUNC(SYSTIMESTAMP));
这导致以下输出:
| 客户ID|名字|姓氏|
| - ------|- ------|- ------|
| 1个|信仰|马扎罗内|
| 第二章|丽莎|萨拉迪诺|
此查询似乎正在工作。
但是我还想显示最后一个“purchase_date”值沿着客户信息,或者如果客户从未购买过产品,则显示NULL,但我似乎不知道如何做到这一点。
有人能帮帮我吗?
下面是重现我的环境的DDL:
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, '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
(CUSTOMER_ID, PRODUCT_ID, QUANTITY, PURCHASE_DATE) AS
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 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;
3条答案
按热度按时间iaqfqrcu1#
您可以使用
ROW_NUMBER
窗口函数来检测每个客户的最后购买日期,然后重新连接到customers信息以获得每个客户的全名。检查here演示。
uklbhaso2#
您可以使用MAX()函数获取每个客户的最近购买日期,然后将该信息与customers表联接,从而显示上次购买日期和客户信息。
lo8azlld3#