【发布时间】:2020-08-13 08:13:35
【问题描述】:
这是来自find out time difference for every user in condition mysql 5.7的继续问题
这是我的小提琴 https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=31b3be9d1e2444eb0b32c262176aa4b4
我有这张桌子
CREATE TABLE test (
ID INT,
user_id INT,
createdAt DATE,
status_id INT
);
INSERT INTO test VALUES
(1, 13, '2020-01-01', 8),
(2, 13, '2020-01-03', 8),
(3, 13, '2020-01-06', 8),
(4, 13, '2020-01-02', 7),
(5, 13, '2020-01-03', 7),
(6, 14, '2020-03-03', 8),
(7, 13, '2020-03-04', 4),
(8, 15, '2020-04-04', 7),
(9, 14, '2020-03-02', 6),
(10, 14, '2020-03-10', 5),
(11, 13, '2020-04-10', 8);
select * from test where status_id != 7
order by createdAt;
+----+---------+------------+-----------+
| ID | user_id | createdAt | status_id |
+----+---------+------------+-----------+
| 1 | 13 | 2020-01-01 | 8 |
| 2 | 13 | 2020-01-03 | 8 |
| 3 | 13 | 2020-01-06 | 8 |
| 9 | 14 | 2020-03-02 | 6 |
| 6 | 14 | 2020-03-03 | 8 |
| 7 | 13 | 2020-03-04 | 4 |
| 10 | 14 | 2020-03-10 | 5 |
+----+---------+------------+-----------+
id是交易的id,user_Id是进行交易的用户的id,createdAt是交易发生的日期,status_id是交易的状态(如果status_Id为7,则交易被拒绝或不批准)。
所以在这种情况下,我想找出“2020-02-01”到“2020-04-01”时间范围内每个重复用户的每个批准交易的时间差,重复用户是正在做的用户在时间范围结束之前的事务,并且在该时间范围内至少再次进行了 1 次事务,在这种情况下,用户在 '2020-04-01' 之前进行了批准事务,并且在这之间至少再次进行了 1 次批准事务2020-02-01' 和 '2020-04-01'。
对于那个问题,我根据@Akina 的回答使用了这个查询
-- Get pairs (current transaction, previous transaction) for these users
SELECT t1.user_id,
t1.createdAt,
t2.createdAt,
DATEDIFF(t2.createdAt, t1.createdAt) diff
-- table for a transaction
FROM test t1
-- table for prev. transaction
JOIN test t2 ON t1.user_id = t2.user_id
AND t1.createdAt < t2.createdAt
AND 7 NOT IN (t1.status_id, t2.status_id)
-- get data only for users from prev. query
JOIN (SELECT t3.user_id
FROM test t3
WHERE t3.status_id != 7
GROUP BY t3.user_id
HAVING SUM(t3.createdAt < '2020-04-01') > 1
AND SUM(t3.createdAt BETWEEN '2020-02-01' AND '2020-04-01')) t4 ON t1.user_id = t4.user_id
-- check that there is no approved transaction between selected transactions
WHERE NOT EXISTS (SELECT NULL
FROM test t5
WHERE t1.user_id = t5.user_id
AND t5.status_id != 7
AND t1.createdAt < t5.createdAt
AND t5.createdAt < t2.createdAt)
the output table was like this
+----------+------------+------------+------+
| user_id | createdAt | createdAt | diff |
+----------+------------+------------+------+
| 13 | 2020-01-01 | 2020-01-03 | 2 |
| 13 | 2020-01-03 | 2020-01-06 | 3 |
| 14 | 2020-03-02 | 2020-03-03 | 1 |
| 13 | 2020-01-06 | 2020-03-04 | 58 |
| 14 | 2020-03-03 | 2020-03-10 | 7 |
+----------+------------+------------+------+
问题是,这个查询为每个用户计算时间范围内的时间差('2020-02-01' 到 '2020-04-01'),并且计算时间范围之前的时间差(参见 users_id 13,用户还计算日期“2020-01-01”到“2020-01-03”的时间差)。我想要的是,如果用户在时间范围之前有交易,我只想计算他的 users_id 在时间范围之前的最后一次交易(在这种情况下,users_id 13 我只想计算'2020-01-06'中的时间差直到 '2020-03-04',因为 2020 年 1 月 6 日是用户在时间范围之前最后一次交易的日期。这样的话,预期的结果是这样的:
+---------+------------+------------+------+
| user_id | createdAt | createdAt | diff |
+---------+------------+------------+------+
| 14 | 2020-03-02 | 2020-03-03 | 1 |
| 13 | 2020-01-06 | 2020-03-04 | 58 |
| 14 | 2020-03-03 | 2020-03-10 | 7 |
+---------+------------+------------+------+
【问题讨论】:
标签: mysql time having-clause