Postgresql - 先group 然后根据不同group,用同一行 不同列展示
2021/12/29 19:09:06
本文主要是介绍Postgresql - 先group 然后根据不同group,用同一行 不同列展示,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
With --1. group aTable as( SELECT "SourceOrderNumber","OperateType",sum("ServiceCatagoryQuota") as "ServiceCatagoryQuota", array_agg("UsedOrderItemNumber") as "UsedOrderItemNumber" FROM orders."a" group by "SourceOrderNumber","OperateType" ), --2. group后 按类型 一行展示 aSplitTable as ( select "SourceOrderNumber", sum(CASE "OperateType" WHEN 0 THEN "ServiceCatagoryQuota" ELSE 0 END ) "RechargeQuota", sum(CASE "OperateType" WHEN 1 THEN "ServiceCatagoryQuota" ELSE 0 END ) "ConsumeQuota", sum(CASE "OperateType" WHEN 3 THEN "ServiceCatagoryQuota" ELSE 0 END ) "RechargeReturnQuota", sum(CASE "OperateType" WHEN 2 THEN "ServiceCatagoryQuota" ELSE 0 END ) "CancelConsumeQuota", sum("ServiceCatagoryQuota") as "LeftQuota" from aTable group by "SourceOrderNumber" ) SELECT * from aSplitTable;
case的其他用法
SELECT po."OrderNumber" as "订单编号", po."Currency" as "币种id", CASE WHEN sc."Currency" =1 THEN 'CNY' WHEN sc."Currency" =2 THEN 'USD' WHEN sc."Currency" =3 THEN 'EUR' WHEN sc."Currency" =4 THEN 'HKD' WHEN sc."Currency" =5 THEN 'GBP' WHEN sc."Currency" =6 THEN 'AUD' WHEN sc."Currency" =7 THEN 'CAD' WHEN sc."Currency" =8 THEN 'JPY' WHEN sc."Currency" =9 THEN 'KRW' WHEN sc."Currency" =10 THEN 'NZD' WHEN sc."Currency" =11 THEN 'RUB' WHEN sc."Currency" =12 THEN 'SGD' ELSE '其他' end AS "币种名称" from po limit 10;
这篇关于Postgresql - 先group 然后根据不同group,用同一行 不同列展示的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-01-05快速清空 PostgreSQL 数据库中的所有表格,让你的数据库重新焕然一新!
- 2024-01-04在PostgreSQL中创建角色:判断角色是否存在并创建
- 2023-05-16PostgreSQL一站式插件推荐 -- pg_enterprise_views
- 2022-11-22PostgreSQL 实时位置跟踪
- 2022-11-22如何将PostgreSQL插件移植到openGauss
- 2022-11-11PostgreSQL:修改数据库用户的密码
- 2022-11-06Windows 环境搭建 PostgreSQL 物理复制高可用架构数据库服务
- 2022-10-27Windows 环境搭建 PostgreSQL 逻辑复制高可用架构数据库服务
- 2022-10-11PostgreSql安装(Windows10版本)
- 2022-09-13PostgreSQL-Network Address类型操作和函数