SQL:带连接的 PIVOT
SQL: PIVOT with join
新手问题:
下面有table
Period Customer Balance
40 1 10
40 2 15
39 1 9
38 1 10
38 2 20
我想订购它,以便每个时期都有一列,
Customer BalancePeriod38 BalancePeriod39 BalancePeriod40
1 10 9 10
2 15 . 20
这可能吗?
您可以透视数据,使用带大小写的聚合:
select customer,
sum(case when period = 38 then balance else 0 end) as balance_period_38,
sum(case when period = 39 then balance else 0 end) as balance_period_39,
sum(case when period = 40 then balance else 0 end) as balance_period_40
from your_table
group by customer;
新手问题:
下面有table
Period Customer Balance
40 1 10
40 2 15
39 1 9
38 1 10
38 2 20
我想订购它,以便每个时期都有一列,
Customer BalancePeriod38 BalancePeriod39 BalancePeriod40
1 10 9 10
2 15 . 20
这可能吗?
您可以透视数据,使用带大小写的聚合:
select customer,
sum(case when period = 38 then balance else 0 end) as balance_period_38,
sum(case when period = 39 then balance else 0 end) as balance_period_39,
sum(case when period = 40 then balance else 0 end) as balance_period_40
from your_table
group by customer;