PostgreSQL教程

Postgresql - 先group 然后根据不同group,用同一行 不同列展示

本文主要是介绍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,用同一行 不同列展示的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!