当前位置: 代码迷 >> SQL >> SQL队列转换方法
  详细解决方案

SQL队列转换方法

热度:74   发布时间:2016-05-05 12:51:13.0
SQL行列转换方法

create table tb(终端名称 varchar(10) , CEI分值 varchar(10) , 终端数量 int)

insert into tb values('三星' , '0-5' , 74)

insert into tb values('三星' , '10-15' , 83)

insert into tb values('苹果' , '0-5' , 93)

insert into tb values('苹果' , '5-10' , 74)

insert into tb values('苹果' , '10-15' , 84)

insert into tb values('山寨' , '0-5' , 222)

go


select 终端名称 as 终端名称 ,

max(case CEI分值 when '0-5' then 终端数量 else 0 end) ?'0-5',

max(case CEI分值 when '5-10' then 终端数量 else 0 end) ?'5-10',

max(case CEI分值 when '10-15' then 终端数量 else 0 end) ?'10-15',

max(case CEI分值 when '15-20' then 终端数量 else 0 end) ?'15-20',

max(case CEI分值 when '20-25' then 终端数量 else 0 end) ?'20-25',

max(case CEI分值 when '25-30' then 终端数量 else 0 end) ?'25-30',

max(case CEI分值 when '30-35' then 终端数量 else 0 end) ?'30-35',

max(case CEI分值 when '35-40' then 终端数量 else 0 end) ?'35-40',

max(case CEI分值 when '40-45' then 终端数量 else 0 end) ?'40-45',

max(case CEI分值 when '45-50' then 终端数量 else 0 end) ?'45-50',

max(case CEI分值 when '50-55' then 终端数量 else 0 end) ?'50-55',

max(case CEI分值 when '55-60' then 终端数量 else 0 end) ?'55-60',

max(case CEI分值 when '60-65' then 终端数量 else 0 end) ?'60-65',

max(case CEI分值 when '65-70' then 终端数量 else 0 end) ?'65-70',

max(case CEI分值 when '70-75' then 终端数量 else 0 end) ?'70-75',

max(case CEI分值 when '75-80' then 终端数量 else 0 end) ?'75-80',

max(case CEI分值 when '80-85' then 终端数量 else 0 end) ?'80-85',

max(case CEI分值 when '85-90' then 终端数量 else 0 end) ?'85-90',

max(case CEI分值 when '90-95' then 终端数量 else 0 end) ?'90-95',

max(case CEI分值 when '95-100' then 终端数量 else 0 end) ?'95-100'

from tb

group by 终端名称

order by 终端名称

  相关解决方案