zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

sql 行转列

SQL 转列
2023-06-13 09:15:37 时间

行转列

fd_name

fd_type

fd_total

a

type1

1

a

type2

2

a

type3

3

转成

fd_name

fd_type1_total

fd_type2_total

fd_type3_total

a

1

2

3

sql

select fd_name
     , max(case when fd_type = 'type1' then fd_total else 0 end) as fd_type1_total
     , max(case when fd_type = 'type2' then fd_total else 0 end) as fd_type2_total
     , max(case when fd_type = 'type3' then fd_total else 0 end) as fd_type3_total
from
    t_mytable
group by fd_name