sql - 通过拆分列的 value 呈现摘要信息

有没有办法通过下面的 table 提出以下内容:

customer_id | loan_date  | loan_amount | loan_paid | status
------------+------------+-------------+-----------+--------
customer1     04/02/2010   5000          3850        active
customer2     04/02/2010   3000          3000        completed
customer3     04/02/2010   6500          4300        defaulted
...

平均贷款,所有贷款的标准差,贷款数量,违约总额,每月收回的贷款总额。 (我有大约 5 年的数据)。

我不知道从哪里开始。

回答1

这样开始:

SELECT date_trunc('month', loan_date)
     , avg(loan_amount) AS avg_loan
     , stddev_samp(loan_amount)  AS stddev_samp
     , count(*) AS ct_loans
     , count(*) FILTER (WHERE status = 'defaulted') AS ct_defaulted
     , sum(loan_paid) AS sum_paid
FROM   tbl
GROUP  BY 1
ORDER  BY 1;

然后细化。细节尚不清楚。不确定 loan_paid 的确切含义,以及您想要准确总结的内容。 https://www.postgresql.org/docs/current/functions-aggregate.html#FUNCTIONS-AGGREGATE-STATISTICS-TABLE的名称下有多种度量...

关于https://www.postgresql.org/docs/current/functions-aggregate.html

关于https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC

关于GROUP BY 1

关于 aggregate FILTER 子句:

相似文章

随机推荐

最新文章