如何仅使用最后一个事务计数timediff mysql 5.7
这是我的问题 这是我的小提琴 我有这张桌子如何仅使用最后一个事务计数timediff mysql 5.7,mysql,time,having-clause,Mysql,Time,Having Clause,这是我的问题 这是我的小提琴 我有这张桌子 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
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”的时间范围内每个重复用户上的每个审批事务的时差,重复用户是在时间范围结束之前进行事务处理的用户,在这种情况下,在时间范围内至少再次进行一次事务处理,用户在“2020-04-01”之前进行审批交易,并且在“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”之间的时间框架开始之前结束的所有交易,因为它们不感兴趣。 因为你已经排除了所有超出时间范围的事情 您仍然知道在事务开始和结束时输入数据的时间,因此您应该在一个额外的列中标记与一起属于的行,这将使您的查询更加简单,并且您的状态不可用,因为它会重复自身 小提琴 更新: 现在这实际上更有意义了
Dfiddle很抱歉,我忘了在2020-04-10之前不将“2020-03-04”之间的用户id传输差异放在“2020-04-10”之间,因为“2020-04-10”超出了“2020-02-01”到“2020-04-10”的范围,我想在最后一个查询中添加并创建比较<'2020-04-10',但它说的是Unknown coloumn createcompare Yes,这更有意义,我已经更新了答案非常感谢先生,这是我想要的,给你奖金,提前谢谢
+---------+------------+------------+------+
| 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 |
+---------+------------+------------+------+
SELECT t1.user_id,
t1.createdAt,
t2.createdAt createcompare,
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)
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
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)
HAViNG createcompare > '2020-02-01'
user_id | createdAt | cretecompare | 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
13 | 2020-03-04 | 2020-04-10 | 37
SELECT t1.user_id,
t1.createdAt cretecompare1,
t2.createdAt cretecompare2,
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)
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
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)
HAViNG cretecompare2 BETWEEN '2020-02-01' AND '2020-04-01'
user_id | cretecompare1 | cretecompare2 | 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