【问题标题】:how to take only last transaction to count timediff mysql 5.7如何仅使用最后一个事务来计算 timediff mysql 5.7
【发布时间】: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


    【解决方案1】:

    我认为您只需排除在时间框架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'
    
    用户 ID |创建于 |比较 |差异 ------: | :--------- | :----------- | ---: 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小提琴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'
    
    用户 ID |比较1 |比较2 |差异 ------: | :------------ | :------------ | ---: 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小提琴here

    【讨论】:

    • 对不起,我忘了在 '2020-03-04' 到 '2020-04-10' 之间放置 user_id trasaction diff,因为 '2020-04-10' 在 '2020- 中超出范围02-01' 直到 '2020-04-10',我想在最后一个查询中添加和 createcompare dbfiddle.uk/…
    • 是的,这更有意义,我已经更新了答案
    • 非常感谢先生,这就是我想要的,赏金给你,谢谢你提前
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    • 2019-01-22
    • 2020-12-19
    相关资源
    最近更新 更多