Google bigquery 在BigQuery中切片帐户余额数据以生成借方报告
我收集了一段时间的账户余额:Google bigquery 在BigQuery中切片帐户余额数据以生成借方报告,google-bigquery,Google Bigquery,我收集了一段时间的账户余额: +-----------------+------------+-------------+-----------------------+ | account_balance | department | customer_id | timestamp | +-----------------+------------+-------------+-----------------------+ | 5 | A
+-----------------+------------+-------------+-----------------------+
| 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每月和每年获取一个部门的借方、贷方和余额贷方借方。4月份的结果应为前几个月的总结:
+---------+--------+-------+------------+-------+--------+
| 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 |
+---------+--------+-------+------------+-------+--------+
客户的账户余额可能不会每个月都发生变化。客户1可能在2月份有账户余额记录,但3月份没有
关于解决办法的说明:
从时间戳月份开始使用EXTRACTMONTH
使用从时间戳年份开始的年份
按月份、年份、部门分组
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
当我创建一个增量总和时,我应该从1中的结果中得到与最后一行相同的值
关于解决办法的说明:
使用account\u balance-LAGaccount\u balance OVERPARTITION BY department ORDER BY timestamp ASC delta计算delta
你的问题还不清楚,但听起来好像你想在任何给定的时间点获得未偿余额
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
以下查询对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中交叉连接的美妙之处 你的问题不清楚,但听起来你想在任何给定的时间点获得未偿余额
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
以下查询对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中交叉连接的美妙之处 我建议您重新审视您的问题,并通过良好的数据示例和预期结果使其更加集中和具体。否则,有人能够如实回答的可能性很小now@MikhailBerlyant谢谢你的反馈!我修改了我的问题。老实说,你对解决方案的注释在这里看起来无关紧要!您更愿意提供您心目中的逻辑的更多细节。例如,如何为两个输出计算这些数字。你可以说这是显而易见的,也许是的,但从我的经验来看,所以我知道总有一些事情,然后碰巧不是OP的意思,所以,例如,对我来说,我不想花时间去想一些东西来改变你的逻辑,然后意识到这不是你的意思——我想我们在座的其他人也有同样的感受。。。。。。这就是为什么到目前为止你还没有看到任何合理的答案。我的想法是,我建议您重新审视您的问题,并通过良好的数据示例和预期结果使其更加集中和具体。否则,有人能够如实回答的可能性很小now@MikhailBerlyant谢谢你的反馈!我修改了我的问题。老实说,你对解决方案的注释在这里看起来无关紧要!您更愿意提供您心目中的逻辑的更多细节。例如,如何为两个输出计算这些数字。你可以说这是显而易见的,也许是的,但从我的经验来看,所以我知道总有一些事情,然后碰巧不是OP的意思,所以,例如,对我来说,我不想花时间去想一些东西来改变你的逻辑,然后意识到这不是你的意思——我想我们在座的其他人也有同样的感受。。。。。。这就是为什么到目前为止你还没有看到任何合理的答案。只是我的想法MRB没有定义。无法在最新余额选择列表表达式引用列余额计算中访问余额计算,该列未分组也未聚合编辑帖子,最新余额CTE需要将group by 1更改为group by 2mrb未定义。无法在最新余额选择列表表达式引用列余额计算中访问余额计算,该列余额计算既未分组也未聚合编辑帖子,最新余额CTE需要将group by 1更改为group by 2