`
chicony
  • 浏览: 122410 次
  • 性别: Icon_minigender_1
  • 来自: 孝感
社区版块
存档分类
最新评论

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 终端名称

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics