计算每天的重复记录(没有 window 功能)

Count repeat records per day (without window functions)

我正在尝试统计每天的回头客记录,但我在使用 MariaDB 10.1 时遇到了一些麻烦,因为 window 函数直到 10.2 才引入(因此没有分区,等级等)

我有一组示例数据,如下所示:

| Date       | Country | Type      | Email         | Response_Time |
| ---------- | ------- | --------- | ------------- | ------------- |
| 2021-05-21 | AU      | Enquiry   | bill@fake.com | 910           |
| 2021-05-21 | AU      | Enquiry   | bill@fake.com | 1050          |
| 2021-05-21 | NZ      | Complaint | jim@fake.com  | 56            |
| 2021-05-22 | NZ      | Enquiry   | jim@fake.com  | 1000          |
| 2021-05-22 | NZ      | Enquiry   | jim@fake.com  | 845           |
| 2021-05-22 | NZ      | Enquiry   | jim@fake.com  | 700           |
| 2021-05-22 | NZ      | Complaint | jim@fake.com  | 217           |
| 2021-05-23 | UK      | Enquiry   | jane@fake.com | 843           |
| 2021-05-23 | NZ      | Enquiry   | jim@fake.com  | 1795          |
| 2021-05-23 | NZ      | Enquiry   | jim@fake.com  | 521           |
| 2021-05-23 | AU      | Complaint | bill@fake.com | 150           |

以上内容可以通过以下查询生成:

SELECT
    DATE(Start_Time) AS "Date",
    Country,
    Type,
    Email,
    Response_Time
FROM EMAIL_DETAIL
WHERE DATE(Start_Time) BETWEEN '2021-05-21' AND '2021-05-23'
AND COUNTRY IN ('AU','NZ','UK')
;

我想统计在日期、国家和类型组中出现多次的电子邮件地址,并将其显示为这样的摘要:

| Country | Type      | Volume | Avg_Response_Time | Repeat_Daily |
| ------- | --------- | ------ | ----------------- | ------------ |
| AU      | Enquiry   | 2      | 980               | 1            |
| AU      | Complaint | 1      | 150               | 0            |
| NZ      | Enquiry   | 5      | 972               | 3            |
| NZ      | Complaint | 1      | 137               | 0            |
| UK      | Enquiry   | 1      | 843               | 0            |

每日重复计数是指电子邮件地址在日期、国家和类型组中出现超过一次的记录计数。数量是每个国家和类型的记录总数。

由于此版本的 MariaDB 中缺少 window 功能,我遇到了困难,非常感谢任何帮助。

(对于格式化为代码的表格表示歉意,否则我在尝试 post 时遇到格式错误)

嗯。 . .我认为这是两个级别的聚合:

SELECT country, type, SUM(cnt) as volume,
       SUM(Total_Response_Time) / SUM(cnt) as avg_Response_time,
       SUM(CASE WHEN cnt > 1 THEN cnt ELSE 0 END) as repeat_daily
FROM (SELECT DATE(Start_Time) AS "Date", Country, Type, Email,
             SUM(Response_Time) as Total_Response_Time, COUNT(*) as cnt
      FROM EMAIL_DETAIL
      WHERE DATE(Start_Time) BETWEEN '2021-05-21' AND '2021-05-23' AND
            COUNTRY IN ('AU','NZ','UK')
      GROUP BY date, country, type, email
     ) ed
GROUP BY country, type
select Date,Country, Type, AVG(Response_Time) ,sum(cc) as Volumn, sum(case when cc>1 then 1 end) as Repeat_Daily
from (
SELECT
    DATE(Start_Time) AS "Date",
    Country,
    Type,
    count(email) cc,    
    AVG(Response_Time) Response_Time
FROM EMAIL_DETAIL
WHERE DATE(Start_Time) BETWEEN '2021-05-21' AND '2021-05-23'
  AND COUNTRY IN ('AU','NZ','UK')
group by 
  DATE(Start_Time) AS "Date",
    Country,Type, email
)
group by "Date",Country,Type