简体   繁体   English

在这种情况下使用 SQL PIVOT?

[英]Use SQL PIVOT in this case?

The scenario is:场景是:

Table name is = dw.tm_usage_monthly表名是 = dw.tm_usage_monthly
Columns are;列是; account ID = unique ID by customer帐户 ID = 客户的唯一 ID
usage_month = is usage month as 201901, 201902 usage_month = 是使用月份,如 201901、201902
uom = Mbps or Nodes uom = Mbps 或节点
usage = usage by month by account_id and uom usage = account_id 和 uom 按月使用

The database has data like below;数据库有如下数据;

在此处输入图像描述

Is it possible to do in SQL?可以在 SQL 中做吗? I'm using SQL workbench.我正在使用 SQL 工作台。

在此处输入图像描述

Just use conditional aggregation:只需使用条件聚合:

select account_id,
       sum(case when month = 201901 and oum = 'Nodes' then usage end) as nodes_201901,
       . . .
       sum(case when month = 201901 and oum = 'Mpbs' then usage end) as mpbs_201901,
       . . .
from t
group by account_id;

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

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