当前位置: 代码迷 >> Sql Server >> 急求解决解决方法
  详细解决方案

急求解决解决方法

热度:27   发布时间:2016-04-24 18:39:07.0
急求解决
这有一大列数据
地市编码   号码        价格      时间
551 13305557479 10 20131231
552 13305557812 30 20140115
552 13305558309 10 20140110
553 13305558875 30 20131224
553 13305558877 30 20130807
553 13305558883 10 20140119
553 13305559200 30 20131225
555 13305559311 10 20131225
555 13305559440 30 20140121
555 13305559531 20 20131223
556 13305560626 20 20140127
556 13305561276 20 20140122
556 13305561288 10 20140110
556 13305562035 10 20140125

要求用sql语言给出各分地市,各价格的数量的统计,请问高人这儿的sql怎么写啊


------解决方案--------------------
完整的例子:
--drop table test

create table test(地市编码 int,   号码 varchar(30),       价格  int,    时间 varchar(10))

insert into test
select 551 ,'13305557479' ,10 ,'20131231' union all
select 552 ,'13305557812' ,30 ,'20140115' union all
select 552 ,'13305558309' ,10 ,'20140110' union all
select 553 ,'13305558875' ,30 ,'20131224' union all
select 553 ,'13305558877' ,30 ,'20130807' union all
select 553 ,'13305558883' ,10 ,'20140119' union all
select 553 ,'13305559200' ,30 ,'20131225' union all
select 555 ,'13305559311' ,10 ,'20131225' union all
select 555 ,'13305559440' ,30 ,'20140121' union all
select 555 ,'13305559531' ,20 ,'20131223' union all
select 556 ,'13305560626' ,20 ,'20140127' union all
select 556 ,'13305561276' ,20 ,'20140122' union all
select 556 ,'13305561288' ,10 ,'20140110' union all
select 556 ,'13305562035' ,10 ,'20140125'
go


select 地市编码,
       COUNT(case when 价格=10 then 1 else null end) as '10元',
       COUNT(case when 价格=20 then 1 else null end) as '20元',
       COUNT(case when 价格=30 then 1 else null end) as '30元',
       COUNT(case when 价格=50 then 1 else null end) as '50元',
       COUNT(case when 价格=100 then 1 else null end) as '100元'
              
from test
group by 地市编码
/*
地市编码 10元 20元 30元 50元 100元
551 1 0 0 0 0
552 1 0 1 0 0
553 1 0 3 0 0
555 1 1 1 0 0
556 2 2 0 0 0
*/

------解决方案--------------------
借用下当家的数据,用我的SQL执行后结果完全一致

  相关解决方案