如何计算 Teradata SQL 中每个客户在选定月份的平均操作数?

How to calculate average number of actions in selected month per client in Teradata SQL?

我在 Teradata SQL 中有 table 个事务,如下所示:

ID   | trans_date
-------------------
123  | 2021-09-15
456  | 2021-10-20
777  | 2021-11-02
890  | 2021-02-14
...  | ...

我需要计算客户在 09、10 和 11 月的平均交易次数,因此我需要如下内容:

Month  | Avg_num_trx
--------------------------------------------------------
09     | *average number of transactions per client in month 09*
10     | *average number of transactions per client in month 10*
11     | *average number of transactions per client in month 11*

我如何在 Teradata 中执行此操作 SQL?

对 Teradata 不太熟悉,您可以先从 trans_date 中提取月份,然后将 id 和月份分组并添加 count(id)。从那里您可以按 avg(count_id) 对月份进行分组。像这样 -

WITH extraction AS(
SELECT 
 ID,
 EXTRACT (MONTH FROM trans_date) AS MM
FROM your_table)

,

WITH id_counter AS(
SELECT
 ID,
 MM,
 COUNT(ID) as id_count
FROM extraction
GROUP BY ID, MM)

SELECT
 MM,
 AVG(id_count) AS Avg_num_trx
FROM id_counter
ORDER BY MM;

第一个 CTE 从 trans_date 抢了一个月。 第二个 CTE 将 ID 和月份与 count(ID) 分组 - 应该为您提供该客户 ID 在该月的总操作数 id_count。 最后的 table 得到按月分组的 id_count 的平均值,这应该是该期间每个客户的平均互动。

如果 EXTRACT 由于某种原因不起作用,您还可以尝试 STRTOK(trans_date, '-', 2).

其他可能的方法来替代-

--current
EXTRACT (MONTH FROM trans_date) AS MM

--option 1
STRTOK(trans_date, '-', 2) AS MM

--option 2
LEFT(RIGHT(trans_date, 5),2) AS MM

以上重写为子查询 - 应该有助于调试 -

SELECT
 MM,
 AVG(id_count) AS Avg_num_trx
FROM (SELECT
       ID,
       MM,
       COUNT(ID) as id_count
       FROM (SELECT 
              ID,
              EXTRACT (MONTH FROM trans_date) AS MM
              FROM your_table) AS a
       GROUP BY ID, MM) AS b
ORDER BY MM;

这将 return 预期的答案:

SELECT 
   Extract (MONTH From trans_date) AS MM,
   Cast(Count(*) AS FLOAT) / Count(DISTINCT id)
FROM my_table
GROUP BY MM

与@procopypaster 的回答进行比较,看看哪一个对您的数据更有效。