oracle 跟踪购买的连续天数(将标记返回到另一列?)

kcugc4gi  于 2023-10-16  发布在  Oracle
关注(0)|答案(1)|浏览(116)

在数据集中,有用户沿着他们从不同公司购买的产品和购买日期。(其他栏目是无关的,现在。)我试图确定用户和他们的购买日期,用户养成了连续几天订购的习惯(从一家公司在多天是坏的,从不同的公司在不同的日子订购是可以的),而不是一次订购所有的东西,以保存运费等。
例如,如果他们在2023.08.23、2023.08.24和2023.08.25上订购,而不是在2023.08.23、2023.08.23和2023.08.23上一次订购所有内容。
我试着在我的数据集上运行这段代码。但它显示了一个不同的输出,我想看到的。你能修改一下我的代码吗?是否有可能以某种方式标记那些日子,在原始表中的用户?这是我的计划与旗帜在我以前张贴的代码(为进一步调查)-图片补充。

WITH t2 AS (
    SELECT USERT, CREATIONDATE, COMPANY ,
    LAG(creationdate) OVER(PARTITION BY USERT ORDER BY USERT) AS prev_diff,
    LEAD(creationdate) OVER(PARTITION BY USERT ORDER BY USERT) AS next_diff,
    LAG(USERT) OVER(ORDER BY USERT) AS prev_USERT,
    LEAD(USERT) OVER(ORDER BY USERT) AS next_USERT
    FROM VEKPOLFA1P72_TESZT_P
)

SELECT USERT, CREATIONDATE, COMPANY,
       CASE
           WHEN ((prev_diff IS NULL OR creationdate - prev_diff <= 1) AND USERT = prev_USERT) OR 
                ((next_diff IS NULL OR next_diff - creationdate <= 1) AND USERT = next_USERT) THEN 'Consecutive'
           ELSE 'Non-Consecutive'
       END AS consecutive_marker
FROM t2
WHERE creationdate >= '20230601' AND creationdate <= '20230630'

数据集示例:

USERT;CREATIONDATE;COMPANY
43014502;20230605;SCHKFT
43014503;20230605;EURFT.
43014509;20230606;HORANS
43014516;20230607;EURFT.
43014516;20230607;EURFT.
43014522;20230620;HORANS
43014523;20230623;GHII K
43014524;20230624;EURFT.
43014533;20230603;HORANS
43014534;20230629;GHII K
45921390;20230629;NANREC
45921390;20230628;NANREC
45921390;20230630;NANREC
45931996;20230630;BEYECT
49117108;20230613;BEYECT
49148157;20230612;D E BT
49148163;20230612;STAFT.
49148165;20230615;MENFT.
49148165;20230615;MENFT.
49148167;20230604;INGMBH
49148167;20230605;INGMBH
49148167;20230606;INGMBH
49148168;20230601;GUT KG
49148174;20230620;PAPRT.
49148174;20230620;FRT.
49148174;20230620;PAPRT.
49148175;20230601;PANOPE
49148175;20230602;FAE
49148175;20230605;PANOPE
49148175;20230605;PANOPE
49148175;20230605;PANOPE
49148179;20230621;GK LGA
49148179;20230622;GK LGA
49148179;20230623;GK LGA
49148179;20230624;GK LGA
49148183;20230601;SCHMBH
49148183;20230601;SCHMBH
49148183;20230630;SCHMBH

预期输出:

USERT;CREATIONDATE;COMPANY
45921390;20230629;NANREC
45921390;20230628;NANREC
45921390;20230630;NANREC
49148167;20230604;INGMBH
49148167;20230605;INGMBH
49148167;20230606;INGMBH
49148179;20230621;GK LGA
49148179;20230622;GK LGA
49148179;20230623;GK LGA
49148179;20230624;GK LGA
...
e4eetjau

e4eetjau1#

From Oracle 12, you can use MATCH_RECOGNIZE to perform row-by-row pattern matching:

SELECT *
FROM   table_name
MATCH_RECOGNIZE(
  PARTITION BY usert
  ORDER     BY creationdate
  MEASURES
    CLASSIFIER() AS consecutive_marker
  ALL ROWS PER MATCH
  PATTERN ("Consecutive"{2,} | "Non-consecutive")
  DEFINE
    "Consecutive" AS PREV(creationdate) + INTERVAL '1' DAY >= creationdate
                  OR NEXT(creationdate) - INTERVAL '1' DAY <= creationdate
)

Or, in earlier versions:

SELECT t.*,
       CASE
       WHEN LAG(creationdate)
              OVER (PARTITION BY usert ORDER BY creationdate, ROWNUM)
              + INTERVAL '1' DAY
              >= creationdate
       OR   LEAD(creationdate)
              OVER (PARTITION BY usert ORDER BY creationdate, ROWNUM)
              - INTERVAL '1' DAY
              <= creationdate
       THEN 'Consecutive'
       ELSE 'Non-consecutive'
       END AS consecutive_marker
FROM   table_name t

Which, for the sample data:

CREATE TABLE table_name (USERT, CREATIONDATE, COMPANY) AS
SELECT 43014502, DATE '2023-06-05', 'SCHKFT' FROM DUAL UNION ALL
SELECT 43014503, DATE '2023-06-05', 'EURFT.' FROM DUAL UNION ALL
SELECT 43014509, DATE '2023-06-06', 'HORANS' FROM DUAL UNION ALL
SELECT 43014516, DATE '2023-06-07', 'EURFT.' FROM DUAL UNION ALL
SELECT 43014516, DATE '2023-06-07', 'EURFT.' FROM DUAL UNION ALL
SELECT 43014522, DATE '2023-06-20', 'HORANS' FROM DUAL UNION ALL
SELECT 43014523, DATE '2023-06-23', 'GHII K' FROM DUAL UNION ALL
SELECT 43014524, DATE '2023-06-24', 'EURFT.' FROM DUAL UNION ALL
SELECT 43014533, DATE '2023-06-03', 'HORANS' FROM DUAL UNION ALL
SELECT 43014534, DATE '2023-06-29', 'GHII K' FROM DUAL UNION ALL
SELECT 45921390, DATE '2023-06-29', 'NANREC' FROM DUAL UNION ALL
SELECT 45921390, DATE '2023-06-28', 'NANREC' FROM DUAL UNION ALL
SELECT 45921390, DATE '2023-06-30', 'NANREC' FROM DUAL UNION ALL
SELECT 45931996, DATE '2023-06-30', 'BEYECT' FROM DUAL UNION ALL
SELECT 49117108, DATE '2023-06-13', 'BEYECT' FROM DUAL UNION ALL
SELECT 49148157, DATE '2023-06-12', 'D E BT' FROM DUAL UNION ALL
SELECT 49148163, DATE '2023-06-12', 'STAFT.' FROM DUAL UNION ALL
SELECT 49148165, DATE '2023-06-15', 'MENFT.' FROM DUAL UNION ALL
SELECT 49148165, DATE '2023-06-15', 'MENFT.' FROM DUAL UNION ALL
SELECT 49148167, DATE '2023-06-04', 'INGMBH' FROM DUAL UNION ALL
SELECT 49148167, DATE '2023-06-05', 'INGMBH' FROM DUAL UNION ALL
SELECT 49148167, DATE '2023-06-06', 'INGMBH' FROM DUAL UNION ALL
SELECT 49148168, DATE '2023-06-01', 'GUT KG' FROM DUAL UNION ALL
SELECT 49148174, DATE '2023-06-20', 'PAPRT.' FROM DUAL UNION ALL
SELECT 49148174, DATE '2023-06-20', 'FRT.'   FROM DUAL UNION ALL
SELECT 49148174, DATE '2023-06-20', 'PAPRT.' FROM DUAL UNION ALL
SELECT 49148175, DATE '2023-06-01', 'PANOPE' FROM DUAL UNION ALL
SELECT 49148175, DATE '2023-06-02', 'FAE'    FROM DUAL UNION ALL
SELECT 49148175, DATE '2023-06-05', 'PANOPE' FROM DUAL UNION ALL
SELECT 49148175, DATE '2023-06-05', 'PANOPE' FROM DUAL UNION ALL
SELECT 49148175, DATE '2023-06-05', 'PANOPE' FROM DUAL UNION ALL
SELECT 49148179, DATE '2023-06-21', 'GK LGA' FROM DUAL UNION ALL
SELECT 49148179, DATE '2023-06-22', 'GK LGA' FROM DUAL UNION ALL
SELECT 49148179, DATE '2023-06-23', 'GK LGA' FROM DUAL UNION ALL
SELECT 49148179, DATE '2023-06-24', 'GK LGA' FROM DUAL UNION ALL
SELECT 49148183, DATE '2023-06-01', 'SCHMBH' FROM DUAL UNION ALL
SELECT 49148183, DATE '2023-06-01', 'SCHMBH' FROM DUAL UNION ALL
SELECT 49148183, DATE '2023-06-30', 'SCHMBH' FROM DUAL;

Both output:
| USERT | CREATIONDATE | CONSECUTIVE_MARKER | COMPANY |
| ------------ | ------------ | ------------ | ------------ |
| 43014502 | 2023-06-05 00:00:00 | Non-consecutive | SCHKFT |
| 43014503 | 2023-06-05 00:00:00 | Non-consecutive | EURFT. |
| 43014509 | 2023-06-06 00:00:00 | Non-consecutive | HORANS |
| 43014516 | 2023-06-07 00:00:00 | Consecutive | EURFT. |
| 43014516 | 2023-06-07 00:00:00 | Consecutive | EURFT. |
| 43014522 | 2023-06-20 00:00:00 | Non-consecutive | HORANS |
| 43014523 | 2023-06-23 00:00:00 | Non-consecutive | GHII K |
| 43014524 | 2023-06-24 00:00:00 | Non-consecutive | EURFT. |
| 43014533 | 2023-06-03 00:00:00 | Non-consecutive | HORANS |
| 43014534 | 2023-06-29 00:00:00 | Non-consecutive | GHII K |
| 45921390 | 2023-06-28 00:00:00 | Consecutive | NANREC |
| 45921390 | 2023-06-29 00:00:00 | Consecutive | NANREC |
| 45921390 | 2023-06-30 00:00:00 | Consecutive | NANREC |
| 45931996 | 2023-06-30 00:00:00 | Non-consecutive | BEYECT |
| 49117108 | 2023-06-13 00:00:00 | Non-consecutive | BEYECT |
| 49148157 | 2023-06-12 00:00:00 | Non-consecutive | D E BT |
| 49148163 | 2023-06-12 00:00:00 | Non-consecutive | STAFT. |
| 49148165 | 2023-06-15 00:00:00 | Consecutive | MENFT. |
| 49148165 | 2023-06-15 00:00:00 | Consecutive | MENFT. |
| 49148167 | 2023-06-04 00:00:00 | Consecutive | INGMBH |
| 49148167 | 2023-06-05 00:00:00 | Consecutive | INGMBH |
| 49148167 | 2023-06-06 00:00:00 | Consecutive | INGMBH |
| 49148168 | 2023-06-01 00:00:00 | Non-consecutive | GUT KG |
| 49148174 | 2023-06-20 00:00:00 | Consecutive | PAPRT. |
| 49148174 | 2023-06-20 00:00:00 | Consecutive | PAPRT. |
| 49148174 | 2023-06-20 00:00:00 | Consecutive | FRT. |
| 49148175 | 2023-06-01 00:00:00 | Consecutive | PANOPE |
| 49148175 | 2023-06-02 00:00:00 | Consecutive | FAE |
| 49148175 | 2023-06-05 00:00:00 | Consecutive | PANOPE |
| 49148175 | 2023-06-05 00:00:00 | Consecutive | PANOPE |
| 49148175 | 2023-06-05 00:00:00 | Consecutive | PANOPE |
| 49148179 | 2023-06-21 00:00:00 | Consecutive | GK LGA |
| 49148179 | 2023-06-22 00:00:00 | Consecutive | GK LGA |
| 49148179 | 2023-06-23 00:00:00 | Consecutive | GK LGA |
| 49148179 | 2023-06-24 00:00:00 | Consecutive | GK LGA |
| 49148183 | 2023-06-01 00:00:00 | Consecutive | SCHMBH |
| 49148183 | 2023-06-01 00:00:00 | Consecutive | SCHMBH |
| 49148183 | 2023-06-30 00:00:00 | Non-consecutive | SCHMBH |
fiddle

相关问题