【问题标题】:Slicing account balance data in BigQuery to generate a debit report在 BigQuery 中分割账户余额数据以生成借记报告
【发布时间】:2019-06-22 06:43:25
【问题描述】:

我收集了一段时间内的帐户余额:

+-----------------+------------+-------------+-----------------------+
| account_balance | department | customer_id |  timestamp            |
+-----------------+------------+-------------+-----------------------+
| 5               | A          | 1           |  2019-02-12T00:00:00  |
| -10             | A          | 1           |  2019-02-13T00:00:00  |
| -35             | A          | 1           |  2019-02-14T00:00:00  |
| 20              | A          | 1           |  2019-02-15T00:00:00  |
+-----------------+------------+-------------+-----------------------+

每条记录显示客户在指定时间戳的总账户余额。账户余额增加,例如从 -35 到 20,当客户用 55 充值他的帐户时。当客户使用服务时,他的帐户余额会减少,例如从 5 到 -10。

我想通过两种方式汇总这些数据:

1) 获取部门每月和每年的借方、贷方和余额(贷方-借方)。四月份的结果应该是之前所有月份的总结:

+---------+--------+-------+------------+-------+--------+
| balance | credit | debit | department | month |  year  |
+---------+--------+-------+------------+-------+--------+
| 5       | 10     | -5    | A          | 1     |  2019  |
| 20      | 32     | -12   | A          | 2     |  2019  |
| 35      | 52     | -17   | A          | 3     |  2019  |
| 51      | 70     | -19   | A          | 4     |  2019  |
+---------+--------+-------+------------+-------+--------+

客户的帐户余额可能不会每个月都发生变化。 2月份可能有客户1的账户余额记录,但3月份没有。

解决方案注意事项:

  • 使用EXTRACT(MONTH from timestamp) month
  • 使用EXTRACT(YEAR from timestamp) year
  • GROUP BY month, year, department

2) 按日期获取部门借方、贷方和余额的变化。

+---------+--------+-------+------------+-------------+
| balance | credit | debit | department |  date       |
+---------+--------+-------+------------+-------------+
| 5       | 10     | -5    | A          | 2019-01-15  |
| 15      | 22     | -7    | A          | 2019-02-15  |
| 15      | 20     | -5    | A          | 2019-03-15  |
| 16      | 18     | -2    | A          | 2019-04-15  |
+---------+--------+-------+------------+-------------+
  51       70       -19

当我创建增量的 SUM 时,我应该从 1) 中的结果中获得与最后一行相同的值。

解决方案注意事项:

  • 使用account_balance - LAG(account_balance) OVER(PARTITION BY department ORDER BY timestamp ASC) delta 计算增量

【问题讨论】:

  • 我建议您重新审视您的问题,并通过良好的数据示例和预期结果使其更加集中和具体。否则,有人能够像现在这样回答它的可能性很小
  • @MikhailBerlyant 感谢您的反馈!我修改了我的问题。
  • 说实话 - 你的 notes toward the solution 在这里看起来无关紧要!您宁愿提供有关您所考虑的逻辑的更多详细信息。例如,如何为两个输出计算这些数字。您可以说这很明显-也许确实如此-但是根据我在 SO 上的经验,我知道总有一些事情恰好不是 OP 的意思-所以,例如,对我来说-我不想要花时间尝试想出一些可以对您的逻辑进行逆向工程的东西,然后意识到这不是您的意思-我认为我们这里的其他人也有同样的感觉...
  • ... 这就是为什么你还没有真正看到任何合理的答案。只是我的想法

标签: google-bigquery


【解决方案1】:

您的问题不清楚,但听起来您想在任何给定时间点获得未结余额。

以下查询会在 1 个时间点执行此操作。

with calendar as (
  select cast('2019-06-01' as timestamp) as balance_calc_ts
),
most_recent_balance as (
  select customer_id, balance_calc_ts,max(timestamp) as most_recent_balance_ts
  from <table>
  cross join calendar
  where timestamp < balance_calc_ts -- or <=
  group by 1,2
)
select t.customer_id, t.account_balance, mrb.balance_calc_ts
from <table> t
inner join most_recent_balance mrb on t.customer_id = mrb.customer_id and t.timestamp = mrb.balance_calc_ts 

如果您需要在一系列时间点进行计算,则需要修改日历 CTE 以返回更多日期。这就是 BQ 中 CROSS JOINS 的美妙之处!

【讨论】:

  • mrb 未定义。在 most_recent_balance 内无法达到 balance_calc_ts(SELECT 列表表达式引用既不分组也不聚合的列 balance_calc_ts)
  • 编辑帖子,most_recent_balance CTE需要将group by 1改为group by 2
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-24
  • 1970-01-01
  • 1970-01-01
  • 2016-09-23
  • 2011-05-21
  • 1970-01-01
相关资源
最近更新 更多