如何解决SQL/银行/交易:收入超过一个月的客户
嗨,我有一张“交易”表,其中包含来自不同客户的少量银行交易。在那个表中,我还有每笔交易的日期以及他们的月平均工资。此外,在不同的表“帐户”上,我可以随时获取这些客户中少数几个的银行帐户余额。
我现在有一个请求,要确定这些客户中哪些客户的收入超过一个月,并检查紧急储蓄/恢复能力。关于如何解决这个问题的任何想法?
表事务的 DDL:
CREATE TABLE transactions (
customer_id uuid NOT NULL,transaction_date varchar(10) NOT NULL,transaction_id varchar NOT NULL,transaction_amount numeric(10,2) NOT NULL,description varchar NULL,account_id uuid NOT NULL
)
表帐户的 DDL:
CREATE TABLE account(
customer_id uuid NOT NULL,account_id uuid NOT NULL,provider_id int8 NOT NULL,account_sub_type varchar NOT NULL,current_balance numeric(10,2) NULL
)
到目前为止,我的 CTE/查询如下所示:
WITH
over_one_income AS (
SELECT customer_id,"month","year",salary,transaction_amount,current_balance from
(
SELECT ctf.customer_id,date_part('month',ctf.date_key::date) as "month",date_part('year',ctf.date_key::date) as "year",round(sum(ctf.amount)/24/30,2) as transaction_amount,round(sm.yearly_net/12) as salary,sum(caf.current_balance) as current_balance
from transactions ctf
join account caf
on ctf.customer_id = caf.customer_id and ctf.account_id = caf.account_id
join customer cf
on cf.customer_id = caf.customer_id
join salary_gross_to_net sm
on cf.annual_gross_salary= sm.yearly_gross
group by ctf.customer_id,ctf.date_key::date),sm.yearly_net
) epc
)
select * from over_one_income
上面的查询输出如下:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。