日期:2014-05-17  浏览次数:20446 次

请教这几句SQL语句该怎么写
有一张电话费用表:
tel_no  cost
13800000000   38
13823400000   56
13800056400   88
13800230000   28
13802300000   18
13822220000   68
13844400000   98
13833330000   35
13822220000   31
13811110000   32

其中
13800000000 
13823400000  
13800056400   
13800230000   
13802300000为行政部的号码,13822220000  13844400000为财务部的电话号码,13833330000 
13822220000 13811110000 为销售部的号码, 我想对这张表进行分组得出下表:
电话号码  部门  费用
13800000000  行政部  38
13823400000  行政部  56
13800056400  行政部 88
13800230000  行政部 28
13802300000  行政部 18
13822220000  财务部 68
13844400000  财务部 98
13833330000  销售部 35
13822220000  销售部 31
13811110000  销售部 32

------解决方案--------------------
create table 电话费用表(tel_no varchar(30),  cost int)

insert into 电话费用表
select '13800000000'   ,38 union all
select '13823400000'   ,56 union all
select '13800056400'   ,88 union all
select '13800230000'   ,28 union all
select '13802300000'   ,18 union all
select '13822220000'   ,68 union all
select '13844400000'   ,98 union all
select '13833330000'   ,35 union all
select '13822220000'   ,31 union all
select '13811110000'   ,32
go

select tel_no as 电话号码,
       case when tel_no in ('13800000000','13823400000','13800056400','13800230000','13802300000')
                 then '行政部'
            when tel_no in ('13822220000','13844400000')
                 then '财务部'
            when tel_no in ('13833330000','13822220000','13811110000')
                 then '销售部'   
       end as 部门 ,