create table 新表
AS
select @i:=@i+1 as NO,NO1,NO2,NO3,MYA1 as MM
from(
select NO1,NO2,'MYA1 ' as NO3,MYA1 from 表
union all
select NO1,NO2,'MYA2 ',MYA2 from 表
union all
select NO1,NO2,'MYA3 ',MYA3 from 表
union all
select NO1,NO2,'MYA4 ',MYA4 from 表
union all
select NO1,NO2,'MYA5 ',MYA5 from 表
union all
select NO1,NO2,'MYA6 ',MYA6 from 表
)a order by NO1,NO2
select * from 新表
就是一个动态的行列转换CREATE TABLE `c_wssb_zz` (
`aa011` varchar(10) default NULL,
`aa001` varchar(20) default NULL,
`aa014` varchar(20) default NULL,
`aa015` varchar(200) default NULL,
`aab001` varchar(20) default NULL,
`aa010` varchar(30) default NULL,
`aae216` date default NULL,
`aa013` varchar(3) default NULL,
`aa016` varchar(10) default NULL,
`aae218` date default NULL,
`aab034` varchar(20) default NULL,
`aab901` varchar(255) default NULL
)
加入这是要转换的表,表名不确定,作为一个参数, aab001、AA011这两个也不确定,也是参数,显示的结果把AA014这一列的内容横过来内容,对应AA015, 最后形成的数据格式如下
AAB034,AA011,AA001,AAB001,max(case when aa014='col1' then aa015 else '' end)as col1,max(case when aa014='col2' then aa015 else '' end)as col2,....max(case when aa014='colN' then aa015 else '' end)as colN,AAE216,AAE218,AA013,AA016
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)