如何在相对时间间隔内计算来自Teradata中唯一位置的唯一订单?

时间:2018-12-28 09:19:19

标签: sql teradata

有一个假设的数据库,每天记录数百万的客户交易。
目的是确定交易中的唯一模式,即以每秒,分钟,每小时等的相对时间范围计算唯一订单,以及每个位置的唯一订单。
还有许多其他变量,但我提供了此摘录以使其简单。

So, basically, I would like to count in relative time frames rather than the 'cardinal' time extract. I'm not sure how to do that. I have searched the different forums but can't work out how to do this in Teradata sql.

真的很感谢您的帮助!

预先感谢

示例数据集

+----------+---------------+----------------------+------+
| Customer |    Product    |       DateTime       | City |
+----------+---------------+----------------------+------+
|        1 | Fortnite      | 2018-10-29 11:18:54  | AL   |
|        1 | PUBG          | 2018-10-29 11:19:42  | AK   |
|        1 | Overwatch     | 2018-10-29 11:19:42  | AZ   |
|        1 | DoTA2         | 2018-10-29 11:19:42  | AR   |
|        1 | CS:GO         | 2018-10-29 11:19:43  | CA   |
|        1 | Rocket league | 2018-10-29 11:19:44  | CO   |
|        1 | PUBG          | 2018-10-29 11:19:46  | AR   |
|        1 | Borderlands2  | 2018-10-29 11:19:46  | CT   |
|        1 | CS:GO         | 2018-10-29 11:19:47  | CA   |
|        1 | Borderlands2  | 2018-10-29 11:19:47. | CT   |
|        1 | Path of exile | 2018-10-29 11:19:51  | CT   |
|        1 | Path of exile | 2018-10-29 11:19:53  | CT   |
|        1 | Hearthstone   | 2018-10-29 11:19:56  | CT   |
|        1 | Hearthstone   | 2018-10-29 11:19:57  | DE   |
|        1 | Wonderputt    | 2018-10-29 11:19:57  | CT   |
+----------+---------------+----------------------+------+

我使用了这段代码,并在下面得到了答案

SELECT  Customer, Product, DateTime, City

    ,COUNT(*) OVER (PARTITION BY Customer, DateTime + INTERVAL '1' SECOND) AS #ofOrders_per_1sec


    ,DENSE_RANK() OVER (PARTITION BY Customer,
        EXTRACT(DAY FROM DateTime), 
        EXTRACT(HOUR FROM DateTime), 
        EXTRACT(MINUTE FROM DateTime), 
        EXTRACT(SECOND FROM DateTime + INTERVAL '1' SECOND) 
        ORDER BY Product ASC) 
    + DENSE_RANK() OVER (PARTITION BY Customer,
        EXTRACT(DAY FROM DateTime), 
        EXTRACT(HOUR FROM DateTime),
        EXTRACT(MINUTE FROM DateTime),
        EXTRACT(MINUTE FROM DateTime),
        EXTRACT(SECOND FROM DateTime + INTERVAL '1' SECOND) ORDER BY Product DESC) - 1 
        AS Unique_Products_per_1sec

        ,DENSE_RANK() OVER (PARTITION BY Customer,
        EXTRACT(DAY FROM DateTime), 
        EXTRACT(HOUR FROM DateTime), 
        EXTRACT(MINUTE FROM DateTime + INTERVAL '1' MINUTE) 
        ORDER BY Product ASC) 
    + DENSE_RANK() OVER (PARTITION BY Customer,
        EXTRACT(DAY FROM DateTime), 
        EXTRACT(HOUR FROM DateTime),
        EXTRACT(MINUTE FROM DateTime),
        EXTRACT(MINUTE FROM DateTime + INTERVAL '1' MINUTE) ORDER BY Product DESC) - 1 
        AS Unique_Products_per_1min 

        ,DENSE_RANK() OVER (PARTITION BY Customer,
        EXTRACT(DAY FROM DateTime), 
        EXTRACT(HOUR FROM DateTime), 
        EXTRACT(MINUTE FROM DateTime), 
        EXTRACT(SECOND FROM DateTime + INTERVAL '1' SECOND) 
        ORDER BY City ASC) 
    + DENSE_RANK() OVER (PARTITION BY Customer,
        EXTRACT(DAY FROM DateTime), 
        EXTRACT(HOUR FROM DateTime),
        EXTRACT(MINUTE FROM DateTime),
        EXTRACT(MINUTE FROM DateTime),
        EXTRACT(SECOND FROM DateTime + INTERVAL '1' SECOND) ORDER BY City DESC) - 1 
        AS Unique_City_per_1sec

    ,DENSE_RANK() OVER (PARTITION BY Customer,
        EXTRACT(DAY FROM DateTime), 
        EXTRACT(HOUR FROM DateTime), 
        EXTRACT(MINUTE FROM DateTime + INTERVAL '1' MINUTE) 
        ORDER BY City ASC) 
    + DENSE_RANK() OVER (PARTITION BY Customer,
        EXTRACT(DAY FROM DateTime), 
        EXTRACT(HOUR FROM DateTime),
        EXTRACT(MINUTE FROM DateTime),
        EXTRACT(MINUTE FROM DateTime + INTERVAL '1' MINUTE) ORDER BY City DESC) - 1 
        AS Unique_City_per_1min
FROM Dataset

上面代码的结果  Current result output

但是,我希望得到以下结果: 理想情况下,以相对的方式计算交易,而不是使用datetime字段中的“基数”或绝对时间xtract。

所需的结果输出 Desired result output

0 个答案:

没有答案