【问题标题】:Getting older balances of credit and debit accounts from transactions table for users in bulk从交易表中为用户批量获取旧的贷方和借方账户余额
【发布时间】:2020-01-08 17:19:57
【问题描述】:
CREATE TABLE `account` (
  `id` bigint(15) NOT NULL AUTO_INCREMENT,
  `account_id` bigint(14) NOT NULL,
  `acc_complete_id` bigint(14) DEFAULT NULL,
  `uuid` varchar(400) NOT NULL,
  `last_updated` datetime DEFAULT NULL,
  `created` datetime DEFAULT NULL,
  `balance` decimal(19,2) DEFAULT '0.00'
)

交易

CREATE TABLE `transaction` (
  `id` bigint(19) NOT NULL AUTO_INCREMENT,
  `created` datetime DEFAULT NULL,
  `amount` decimal(19,2) DEFAULT '0.00',
  `debit` bigint(14) DEFAULT NULL,
  `credit` bigint(14) DEFAULT NULL,
  `credit_bal` decimal(19,2) DEFAULT '0.00',
  `debit_bal` decimal(19,2) DEFAULT '0.00'
 )
  • 账户表中的Id字段用1填充,一组0为交易表的借方列。信用也是如此。

  • 交易表维护 txn 中涉及的用户的借方和贷方条目。

  • 最新的用户余额存储在 account.balance 中。
  • 每笔交易结束时的用户余额都存在于 debit_bal 和 credit_bal 中。

现在,我们需要找到系统中大量用户的旧余额。

为此,我们正在尝试获取每个用户的最新借方余额和最新贷方余额 -

但是,这不是为每个 uuid 排序创建日期,而是在最终结果集中排序,因此没有意义 -

SELECT pa.uuid
     , pt.debit_bal
     , pt.created 
  FROM consumer.account pa force index (account_id_UNIQUE) 
  JOIN consumer.transaction pt 
    ON pa.account_id+ 100000000000 = pt.debit 
 GROUP 
    BY pa.uuid 
 ORDER 
    BY pt.created desc;

一些样本数据 -

账户表-

+------+------------+-----------------+---------------------+---------------------+---------------------+---------+
| id   | account_id | acc_complete_id | uuid                | last_updated        | created             | balance |
+------+------------+-----------------+---------------------+---------------------+---------------------+---------+
|   29 |      50536 |    100000050536 | 1026651502611722400 | 2020-01-09 12:43:49 | 2018-01-01 00:00:01 | 2092.10 |
| 1337 |      53071 |    100000053071 | 7266704751953077361 | 2019-12-26 11:45:54 | 2019-10-22 18:13:21 |   99.00 |
|   30 |      50673 |    100000050673 | 8799857402485889540 | 2020-01-05 13:21:16 | 2017-01-01 00:00:01 | 2166.10 |
+------+------------+-----------------+---------------------+---------------------+---------------------+---------+

交易

+---------+---------------------+--------+--------------+--------------+-----------+------------+
| id      | created             | amount | debit        | credit       | debit_bal | credit_bal |
+---------+---------------------+--------+--------------+--------------+-----------+------------+
| 2001705 | 2019-12-07 14:14:18 |   1.00 | 100000050536 |            3 |   2092.00 | 2332445.91 |
| 2001869 | 2020-05-08 14:29:00 |   4.00 | 100000050673 | 200000052870 |   2088.10 |       4.00 |
| 2001874 | 2020-05-09 14:45:04 |   4.00 | 100000050673 | 200000052870 |   2084.10 |       8.00 |
| 2001875 | 2020-05-09 14:46:37 |   4.00 | 100000050673 | 200000052870 |   2080.10 |      12.00 |
| 2002018 | 2019-11-29 18:05:41 |  50.00 | 100000053071 | 300000050673 |      0.00 |    2170.10 |
| 2002019 | 2019-11-29 18:07:41 |   1.00 | 100000053071 | 300000050673 |    100.00 |    2170.10 |
| 2002020 | 2019-11-29 18:07:56 |   1.00 | 100000053071 |            5 |    100.00 |  580037.00 |
| 2002021 | 2019-11-29 18:15:22 |   1.00 | 100000053071 |            5 |    100.00 |  580037.00 |
| 2002022 | 2019-11-29 18:18:45 |   1.00 | 100000053071 |            5 |    100.00 |  580037.00 |
| 2002023 | 2019-11-29 18:20:41 |   1.00 | 100000053071 |            5 |    100.00 |  580037.00 |
| 2002024 | 2019-11-29 18:24:18 |   1.00 | 100000053071 |            5 |    100.00 |  580037.00 |
| 2002025 | 2019-11-29 18:26:19 |   1.00 | 100000053071 |            5 |    100.00 |  580037.00 |
| 2002026 | 2019-11-29 18:28:41 |   1.00 | 100000053071 |            5 |    100.00 |  580037.00 |
| 2002027 | 2019-11-29 18:29:37 |   1.00 | 100000053071 |            5 |    100.00 |  580037.00 |
| 2002028 | 2019-11-29 18:30:40 |   1.00 | 100000053071 |            5 |    100.00 |  580037.00 |
| 2002029 | 2019-11-29 18:35:55 |   1.00 | 100000053071 |            5 |    100.00 |  580037.00 |
| 2002030 | 2019-11-29 18:42:16 |   1.00 | 100000053071 |            5 |    100.00 |  580037.00 |
| 2002031 | 2019-12-02 13:12:01 |   1.00 | 100000053071 |            5 |    100.00 |  580037.00 |
| 2002032 | 2019-12-02 13:18:21 |   1.00 | 100000053071 |            5 |    100.00 |  580037.00 |
| 2002033 | 2019-12-02 13:27:53 |   1.00 | 100000053071 |            5 |    100.00 |  580037.00 |
| 2002034 | 2019-12-02 13:38:11 |   1.00 | 100000053071 |            5 |     99.00 |  580038.00 |
+---------+---------------------+--------+--------------+--------------+-----------+------------+

注意 - 无法导出到 csv 文件,因为我遇到错误 1290 (HY000):MySQL 服务器正在使用 --secure-file-priv 选项运行,因此无法执行此语句

我得到的结果是 -

SELECT pa.uuid
     , pt.debit_bal
     , pt.created 
  FROM consumer.account pa force index (account_id_UNIQUE) 
  JOIN consumer.transaction pt 
    ON pa.account_id+ 100000000000 = pt.debit 
 GROUP 
    BY pa.uuid 
 ORDER 
    BY pt.created DESC;
+---------------------+-----------+---------------------+
| uuid                | debit_bal | created             |
+---------------------+-----------+---------------------+
| 8799857402485889540 |   2088.10 | 2020-05-08 14:29:00 |
| 1026651502611722400 |   2092.00 | 2019-12-07 14:14:18 |
| 7266704751953077361 |      0.00 | 2019-11-29 18:05:41 |
+---------------------+-----------+---------------------+

对于用户 8799857402485889540,2088.10 不是最新余额。实际上,2080.10 是。此查询不是按每个 uuid 的创建日期排序,而是按所有用户的最终结果集排序,因此没有意义。

更新

也试过了 -

 SELECT pa.uuid, 
       pt.debit_bal, 
       Max(pt.created) 
FROM   consumer.account pa FORCE INDEX (account_id_unique) 
       JOIN consumer.transaction pt 
         ON ( pa.account_id + 100000000000 ) = pt.debit 
GROUP  BY pa.uuid; 

它返回最新的 txn 日期,但不返回关联的 debit_bal。

   +---------------------+-----------+---------------------+
| uuid                | debit_bal | max(pt.created)     |
+---------------------+-----------+---------------------+
| 1026651502611722400 |   2092.00 | 2019-12-07 14:14:18 |
| 7266704751953077361 |      0.00 | 2019-12-02 13:38:11 |
| 8799857402485889540 |   2088.10 | 2020-05-09 14:46:37 |
+---------------------+-----------+---------------------+

【问题讨论】:

  • 请注意,DISTINCT 不是函数,并且很少(如果有的话)与 GROUP BY 子句一起使用(作为聚合函数的修饰符除外)
  • 最后,请注意,在没有任何聚合函数的情况下,GROUP BY 子句永远不合适。
  • @Strawberry 在更新中添加了聚合。

标签: mysql group-by aggregate


【解决方案1】:

我应该说数据组织不善。最好根据您的要求重新设计。无论如何都要根据数据...

distinct(pa.uuid) 与 select 查询中的其他列结合将导致从同一 uuid 中获取一行,然后根据给定的条件组装其他列,这 根本不是预期的

取而代之的是,只需使用 pa.uuid 和其他必需的列,并且可以添加 max(credited) 作为标准。

编辑 1:

您正在分组,这将安排行并到达不需要的行。试试这个。

select pa.uuid, pt.debit_bal, pt.created
from consumer.account pa join...on...
where pt.created= 
  (select max(pt1.created) from consumer.transaction pt1 
    where pt1.uuid= pa.uuid)

【讨论】:

  • 检查答案了吗?
猜你喜欢
  • 1970-01-01
  • 2022-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多