通过拆分列的值来呈现摘要信息

Presenting summary information with splitting the value of a column

有没有办法用下面的 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年的数据)。

我不知道从哪里开始。

这样开始:

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 的确切含义,以及您想要准确求和的内容。在 "standard deviation" ...

的名称下有多项措施

关于aggregate functions

关于date_trunc()

关于GROUP BY 1

  • Concatenate multiple result rows of one column into one, group by another column

关于聚合 FILTER 子句:

  • Aggregate columns with additional (distinct) filters