简体   繁体   English

显示客户每天的花费以及他们是否在前一天花费过 (SQL)

[英]Show customer spend per day and whether they have spent the previous day (SQL)

I am trying to create a new row for each customer who spends per day, and a column that indicates whether they spent money the day before or not.我正在尝试为每天花费的每个客户创建一个新行,以及一个指示他们前一天是否花钱的列。 If a customer spends twice a day, they'd still only have 1 row in the table.如果客户每天消费两次,他们的表中仍然只有 1 行。 If the customer spent money the previous day, then it would show up as TRUE.如果客户在前一天花钱,那么它将显示为 TRUE。

This is the original table below:这是下面的原始表格:

+---------------------+-------------+-----------------+
| 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        |
+---------------------+-------------+-----------------+

This is the CREATE statement:这是 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);

Here's what I've got so far:这是我到目前为止所得到的:

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

This code is not working at the moment, but I'm trying to fix it as best I can.这段代码目前不起作用,但我正在尽我所能修复它。

I've had a look through some of the posts, but the closest I could find was this: count transaction per day我浏览了一些帖子,但我能找到的最接近的是: count transaction per day

I'm trying to produce a table that looks like this:我正在尝试生成一个如下所示的表格:

+------------+-------------+--------------------+
| 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              |
+------------+-------------+--------------------+

Edit: This is the current code I'm using, based on the advice I've received.编辑:这是我正在使用的当前代码,基于我收到的建议。

 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

This is the resulting table:这是结果表:

+------------+-------------+--------------------+
| 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                  |
+------------+-------------+--------------------+

I've tried to do a GROUP BY day, customer_id , but it comes up as an error.我尝试按GROUP BY day, customer_id ,但它出现了错误。

Assuming that customers do not make multiple purchases on the same day, just use lag() :假设客户没有在同一天进行多次购买,只需使用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;

If you can have duplicates, then try this instead of lag() :如果你可以有重复,那么试试这个而不是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

EDIT:编辑:

If you want one row per customer per day:如果您希望每位客户每天一排:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM