- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图为每个每天消费的客户创建一个新行,并创建一个列来指示他们前一天是否花钱。如果客户每天消费两次,他们的表中仍然只有 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/
我想使用我编写的类模块的事件。类模块如下所示 ''CError64Row Public Event ErrorClicked(ByVal row As Integer, ByVal column As
我正在寻找实现智能架构的良好实践,以及处理针对具有许多不同 wdsl web 服务的系统的集成的方法。 我已经有 2 年的爱好使用 C# 进行开发了~,因此我并不总是使用正确的术语,但我会尝试描述我正
目前,我正在为我的程序使用 Azure Consumer API。但它非常慢,几乎需要8秒才能给出响应。我现在应该怎么做?这是我正在使用的 azure API.. https://management
我的流程是: AcitveMQ 控制台在主题部分下显示了一个使用者,但是一旦
我一直在阅读类似 Why does a function that accepts a Box complain of a value being moved when a function that
AMQP 函数 consume() 是一个带有回调的阻塞函数,是否可以为 consume() 函数设置超时,以便在特定时间后不再阻塞并且代码执行完成? 最佳答案 是的,方法如下: $amqp = ne
我有一个客户端/服务器应用程序,其中客户端以 JSON 形式将对象发送到运行 PHP 脚本的服务器,然后将此数据放入数据库。 问题是解码是用 json_decode 函数完成的,它似乎适用于字符串而不
所以我已经模拟了我的生产者消费者问题并且我有下面的代码。我的问题是:如果消费者一直处于 while(true) 状态,他如何停止。 在下面的代码中,我添加了 i
我无法使用在delphi 中开发的dll 的功能。我在类型转换方面遇到了一些困难。 这是我要调用 DLL 的函数: function rData(ID: Cardinal; queue: WideSt
我想使用 Unity3D 可视化 Kafka 流。在 Unity 中访问数据流的最佳方式是什么? 我已经用 Node 和 C# 编写了基本使用者,但我不确定如何将它们合并到 Unity 中。任何帮助表
如果标题太笼统,我很抱歉,但我已经浏览了一个小时的互联网,但找不到任何架构解释。我对 RSS 和 Atom 协议(protocol)都是全新的,据我到目前为止所了解的是: 服务器发布文档 客户端订阅此
我很喜欢我刚刚发现的 Guzzle 框架。我正在使用它使用不同的响应结构跨多个 API 聚合数据。它可以使用 JSON 和 XML 找到,但我需要使用的服务之一使用 SOAP。是否有使用 Guzzle
有没有一种方法可以像访问 Microsoft.Azure.Management.Fluent 一样访问 Azure.Management.Conclusion.Models? 当我执行以下代码时,我看
我有这个部分场景图树: CustomPane (with onMouseClicked Handler) → ChildNode (with onMousePressed Handler) 当我在
我的问题是这个 json。 http://dev-rexolution.pantheonsite.io/api/noticias 我只需要使用 vuejs 2 使用数组的第一个元素才能显示它,使用我工
我是 ML 新手,一直在研究 CNTK 教程。我已经成功训练了几个模型。 我完成了迁移学习教程 ( https://github.com/Microsoft/CNTK/blob/v2.1/Tutori
我是 RabbitMq 和 AMQP 的新手,但我对 ActiveMQ 和 JMS 有一些经验。我尝试在主题(JMS 中的主题之类的主题)中发布一条消息,并从多个监听器中使用此消息。比如我发布一条消息
我正在尝试让我的服务器解析以下 JSON: {"hardwareId":1,"registerTime":"2017-02-14T03:42:11.482Z","sensorId":1,"temper
我正在开发一个从外部 url 使用 json 的网站,我试过了但是我得到了一个错误 XMLHttpRequest 无法加载 http://reuniyo.com/tst/json.php。 Acces
我正在尝试使用Kafka Streams(即不是简单的Kafka Consumer)从重试主题中读取之前无法处理的事件。我希望从重试主题中进行消费,如果处理仍然失败(例如,如果外部系统已关闭),我希望
我是一名优秀的程序员,十分优秀!