mysql - 显示客户每天的支出以及他们前一天是否消费过 (SQL)

标签 mysql sql mysql-8.0

我试图为每个每天消费的客户创建一个新行,并创建一个列来指示他们前一天是否花钱。如果客户每天消费两次,他们的表中仍然只有 1 行。如果客户前一天花钱了,那么它会显示为 TRUE。

这是下面的原始表格:

+---------------------+-------------+-----------------+
| datetime            | customer_id | amount          |
+---------------------+-------------+-----------------+
| 2018-03-01 03:00:00 | 3786        | 14.00000        |
| 2018-03-02 17:00:00 | 5678        | 25.00000        |
| 2018-07-09 18:00:00 | 5647        | 1000.99000      |
| 2018-08-17 19:00:00 | 5267        | 45.00000        |
| 2018-08-25 08:00:00 | 3456        | 78.00000        |
| 2018-08-25 17:00:00 | 3456        | 25.00000        |
| 2018-08-26 03:00:00 | 3456        | 34.90000        |
| 2019-02-03 08:00:00 | 3468        | 0.00000         |
| 2019-03-09 06:00:00 | 1111        | 100.00000       |
| 2019-05-25 14:00:00 | 3456        | 15.00000        |
| 2019-07-02 14:00:00 | 88889       | 45.00000        |
| 2019-07-04 03:00:00 | 8979        | 9.00000         |
| 2019-07-09 14:00:00 | 4567        | 9.99000         |
| 2019-08-25 08:00:00 | 1234        | 88.00000        |
| 2019-08-30 09:31:00 | 1234        | 30.00000        |
| 2019-08-30 12:00:00 | 9876        | 55.00000        |
| 2019-09-01 13:00:00 | 88889       | 23.00000        |
+---------------------+-------------+-----------------+

这是 CREATE 语句:

CREATE TABLE IF NOT EXISTS `spend` ( `datetime` datetime NOT NULL, `customer_id` int(11) NOT NULL, `amount` decimal(10, 5) NOT NULL, PRIMARY KEY (`datetime`)) DEFAULT CHARSET=utf8mb4;
INSERT INTO `spend` (`datetime`, `customer_id`, `amount`) VALUES ('2018-03-01 03:00:00', 3786, 14.00000);
INSERT INTO `spend` (`datetime`, `customer_id`, `amount`) VALUES ('2018-03-02 17:00:00', 5678, 25.00000);
INSERT INTO `spend` (`datetime`, `customer_id`, `amount`) VALUES ('2018-07-09 18:00:00', 5647, 1000.99000);
INSERT INTO `spend` (`datetime`, `customer_id`, `amount`) VALUES ('2018-08-17 19:00:00', 5267, 45.00000);
INSERT INTO `spend` (`datetime`, `customer_id`, `amount`) VALUES ('2018-08-25 08:00:00', 3456, 78.00000);
INSERT INTO `spend` (`datetime`, `customer_id`, `amount`) VALUES ('2018-08-25 17:00:00', 3456, 25.00000);
INSERT INTO `spend` (`datetime`, `customer_id`, `amount`) VALUES ('2018-08-26 03:00:00', 3456, 34.90000);
INSERT INTO `spend` (`datetime`, `customer_id`, `amount`) VALUES ('2019-02-03 08:00:00', 3468, 0.00000);
INSERT INTO `spend` (`datetime`, `customer_id`, `amount`) VALUES ('2019-03-09 06:00:00', 1111, 100.00000);
INSERT INTO `spend` (`datetime`, `customer_id`, `amount`) VALUES ('2019-05-25 14:00:00', 3456, 15.00000);
INSERT INTO `spend` (`datetime`, `customer_id`, `amount`) VALUES ('2019-07-02 14:00:00', 88889, 45.00000);
INSERT INTO `spend` (`datetime`, `customer_id`, `amount`) VALUES ('2019-07-04 03:00:00', 8979, 9.00000);
INSERT INTO `spend` (`datetime`, `customer_id`, `amount`) VALUES ('2019-07-09 14:00:00', 4567, 9.99000);
INSERT INTO `spend` (`datetime`, `customer_id`, `amount`) VALUES ('2019-08-25 08:00:00', 1234, 88.00000);
INSERT INTO `spend` (`datetime`, `customer_id`, `amount`) VALUES ('2019-08-30 09:31:00', 1234, 30.00000);
INSERT INTO `spend` (`datetime`, `customer_id`, `amount`) VALUES ('2019-08-30 12:00:00', 9876, 55.00000);
INSERT INTO `spend` (`datetime`, `customer_id`, `amount`) VALUES ('2019-09-01 13:00:00', 88889, 23.00000);

这是我到目前为止所得到的:

SELECT CAST(datetime AS DATE) AS day, 
       COUNT(DISTINCT customer_id) AS daily_spend,
FROM spend
WHERE amount is not NULL
ORDER BY date;

此代码目前无法运行,但我正在尽力修复它。

我浏览了一些帖子,但我能找到的最接近的是:count transaction per day

我正在尝试生成一个如下所示的表格:

+------------+-------------+--------------------+
| day        | customer_id | spent_previous_day |
+------------+-------------+--------------------+
| 2018-03-01 | 3786        | FALSE              |
+------------+-------------+--------------------+
| 2018-03-02 | 5678        | FALSE              |
+------------+-------------+--------------------+
| 2018-07-09 | 5647        | FALSE              |
+------------+-------------+--------------------+
| 2018-08-17 | 5267        | FALSE              |
+------------+-------------+--------------------+
| 2018-08-25 | 3456        | FALSE              |
+------------+-------------+--------------------+
| 2018-08-26 | 3456        | TRUE               |
+------------+-------------+--------------------+
| 2019-02-03 | 3468        | FALSE              |
+------------+-------------+--------------------+
| 2019-03-09 | 1111        | FALSE              |
+------------+-------------+--------------------+
| 2019-05-25 | 3456        | FALSE              |
+------------+-------------+--------------------+
| 2019-07-02 | 88889       | FALSE              |
+------------+-------------+--------------------+
| 2019-07-04 | 8979        | FALSE              |
+------------+-------------+--------------------+
| 2019-07-09 | 4567        | FALSE              |
+------------+-------------+--------------------+
| 2019-08-25 | 1234        | FALSE              |
+------------+-------------+--------------------+
| 2019-08-30 | 1234        | FALSE              |
+------------+-------------+--------------------+
| 2019-08-30 | 9876        | FALSE              |
+------------+-------------+--------------------+
| 2019-09-01 | 88889       | FALSE              |
+------------+-------------+--------------------+

编辑: 这是我当前使用的代码,基于我收到的建议。

select customer_id, CAST(datetime AS DATE) AS day,
      max(date(datetime))  over (partition by customer_id 
                          order by CAST(datetime AS DATE)
                          range between interval 1 day preceding and interval 1 day preceding
                         ) is not null AS spent_previous_day
from spend

这是结果表:

+------------+-------------+--------------------+
| day        | customer_id | spent_previous_day |
+------------+-------------+--------------------+
| 2019-03-09 | 1111        | 0                  |
+------------+-------------+--------------------+
| 2019-08-25 | 1234        | 0                  |
+------------+-------------+--------------------+
| 2019-08-30 | 1234        | 0                  |
+------------+-------------+--------------------+
| 2018-08-25 | 3456        | 0                  |
+------------+-------------+--------------------+
| 2018-08-25 | 3456        | 0                  |
+------------+-------------+--------------------+
| 2018-08-26 | 3456        | 1                  |
+------------+-------------+--------------------+
| 2019-05-25 | 3456        | 0                  |
+------------+-------------+--------------------+
| 2019-02-03 | 3468        | 0                  |
+------------+-------------+--------------------+

我尝试执行GROUP BY day, customer_id,但出现错误。

最佳答案

假设客户在同一天没有多次购买,只需使用lag():

select t.*,
       ( date(lag(datetime) over (partition by customer_id order by datetime)) = date(datetime) - interval 1 day
       ) as prev_day_flag
from spend t;

如果可以有重复项,请尝试使用此方法代替 lag():

max(date(datetime)) over (partition by customer_id
                          order by date(datetime) 
                          range between interval 1 day preceding and interval 1 day preceding
                         ) is not null

编辑:

如果您希望每个客户每天一行:

select s.*,
       ( date(lag(dte) over (partition by customer_id order by dte)) = dte - interval 1 day
       ) as prev_day_flag
from (select customer_id, date(datetime) as dte, sum(amount) as amount
      from spend s
      group by customer_id, date(datetime)
     ) s;

关于mysql - 显示客户每天的支出以及他们前一天是否消费过 (SQL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58192163/

相关文章:

sql - 我正在尝试复制文件,但收到错误消息

mysql - 查询运行太慢

mysql - 如何在 MySQL v8 中生成固定数量的行来进行性能测试?

MySQL 8.0 lower_case_table_names 1

mysql - 如何使我的 WHERE 子句在 SQL 中运行时不会出现语法错误?

asp.net - MySql 音频库

php - 小于或等于不在 codeigniter 中工作

PHP 数组没有返回值

asp.net - 在 c# asp.net 中执行查询

mysql - MySQL 8 中的 "EXPLAIN PARTITIONS"