横表有这么四列,Id是主键, Id code1 code2 code3 111 aaa bbb ccc 纵表只有两列: ID code 111 aaa 111 bbb 111 ccc SQL模板1: select id,max(case when code='aaa' then code end) as code1, max(case when code='bbb' then code end) as code2, max(case when code='ccc' then code end) as code3 from tt group by id SQL模板2: select id,max(case code when 'aaa' then code else null end) as code1, max(case code when 'bbb' then code else null end) as code2, max(case code when 'ccc' then code else null end) as code3 from tt group by id

评论