如何使用 Postgres 做到这一点?
How to do this with Postgres?
我有一个 table 这样的结构
id status action amount fid
1 P Yes 3000 10
2 P No 2000 10
3 P Yes 6000 10
4 D Yes 500 10
5 D No 600 10
6 V Yes 800 10
我想用 --> status P action Yes,status D action No,status V action Yes 过滤一次。结果将基于 fid.
这样输出---->
amount(name1) amount(name2) amount(name3)
9000 600 800
假设您只打算报告上面示例数据中显示的三种状态,我们可以尝试:
SELECT
fid,
SUM(amount) FILTER (WHERE status = 'P' AND action = 'Yes') AS amount_1,
SUM(amount) FILTER (WHERE status = 'D' AND action = 'No') AS amount_2,
SUM(amount) FILTER (WHERE status = 'V' AND action = 'Yes') AS amount_3
FROM yourTable
GROUP BY fid;
我有一个 table 这样的结构
id status action amount fid
1 P Yes 3000 10
2 P No 2000 10
3 P Yes 6000 10
4 D Yes 500 10
5 D No 600 10
6 V Yes 800 10
我想用 --> status P action Yes,status D action No,status V action Yes 过滤一次。结果将基于 fid.
这样输出---->
amount(name1) amount(name2) amount(name3)
9000 600 800
假设您只打算报告上面示例数据中显示的三种状态,我们可以尝试:
SELECT
fid,
SUM(amount) FILTER (WHERE status = 'P' AND action = 'Yes') AS amount_1,
SUM(amount) FILTER (WHERE status = 'D' AND action = 'No') AS amount_2,
SUM(amount) FILTER (WHERE status = 'V' AND action = 'Yes') AS amount_3
FROM yourTable
GROUP BY fid;