mysql - 如何只取最后一笔交易来计算 timediff mysql 5.7

标签 mysql time having-clause

这是来自 find out time difference for every user in condition mysql 5.7 的继续问题

这是我的 fiddle https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=31b3be9d1e2444eb0b32c262176aa4b4

我有这张 table

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' 之前进行批准交易,并且至少在 '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 在时间范围之前的最后一笔交易(在这种情况下,我只想计算“2020-01-06”中的时差的 users_id 13直到 '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 |
+---------+------------+------------+------+

最佳答案

我认为您只需排除在时间范围 BETWEEN '2020-02-01' AND '2020-04-01 开始之前结束的每笔交易,因为它们不的兴趣。 因为您已经排除了超出时间范围的所有内容

您仍然知道在事务开始和结束时输入数据的时间,因此您应该在一个额外的列中标记属于一起的行,这将使您的查询更简单,除了您的状态不可用,因为它重复自己

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

db<> fiddle here

更新:

现在这实际上更有意义

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

db<> fiddle here

关于mysql - 如何只取最后一笔交易来计算 timediff mysql 5.7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63390863/

相关文章:

java - 如何在Java EE环境中使用MySql处理外来字符

mysql - 在 PHPMyAdmin 更新后恢复数据

C++ 使用 std::chrono 以一种很好的方式测量成员函数的执行

java - 使用 SimpleDateFormat 作为文件名字符串

mysql - GROUP BY 已与两个子查询分组的另一个表

sql - 拥有两本书以上

mysql - 根据测试用例排除结果

php - 如果表中不存在行,则 MYSQL 查询不起作用

mysql - 在 MySQL 中的同一语句中使用 GROUP_CONCAT、GROUP 和 HAVING

python - 使用 Pygame 和 Pickle 加载之前耗时