sql 列转行-pivot

发布时间 2023-07-25 16:59:19作者: Shilo

将table_1表中的数据,每个area的每个atype为A、B、C、D的数量统计出来。
重要的点:
---- 1)pivot()里面for前面必须用聚合函数;
---- 2)m字表中的每个字段都会在查询结果里面;

select * from 
    (select area,atype from table_1) m
    pivot (count(1) for atype in ('A','B','C','D'))
    order by area;