简体   繁体   English

Postgres SQL 如何在 SQL 数据库上透视表

[英]Postgres SQL How to Pivot table on SQL database

Please help how to create sql query from this:请帮助如何从这里创建 sql 查询:

原始数据

我想得到的预期输出

Like Pivot function in excel像excel中的Pivot函数

Thank you谢谢

You can do conditional aggregation.您可以进行条件聚合。 The logic is:逻辑是:

select
    date,
    docnumber,
    source,
    doctype,
    notes,
    sum(total) where(account = 'Creditors') creditors,
    sum(total) where(account = 'GST') gst,
    sum(total) where(account = 'Expenses') expenses
from mytable 
group by
    date,
    docnumber,
    source,
    doctype,
    notes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM