嗨,我有一张表“交易”,其中很少有来自不同客户的银行交易。在该表中,我还有每笔交易的日期以及他们的月平均工资。此外,在不同的表“帐户”上,我可以随时获取其中少数客户的银行帐户余额。
我现在有一个请求,要弄清楚这些客户中哪些人的收入超过一个月,并检查紧急储蓄/弹性。关于如何解决这个问题的任何想法?
表事务的 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
,date_part('month',ctf.date_key::date)
,date_part('year',ctf.date_key::date)
,sm.yearly_net
) epc
)
select * from over_one_income
上面的查询输出以下内容: