sql笔试

请用一个sql语句得出结果 
从table1,table2中取出如table3所列格式数据,注意提供的数据及结果不准确,只是作为一个格式向大家请教。
如使用存储过程也可以。

table1

月份mon 部门dep 业绩yj
-------------------------------
一月份 01 10
一月份 02 10
一月份 03 5
二月份 02 8
二月份 04 9
三月份 03 8

table2

部门dep 部门名称dname
--------------------------------
01 国内业务一部
02 国内业务二部
03 国内业务三部
04 国际业务部

table3 (result)

部门dep 一月份 二月份 三月份
--------------------------------------
01 10 null null
02 10 8 null
03 null 5 8
04 null null 9

------------------------------------------
1)
select a.部门名称dname,b.业绩yj as '一月份',c.业绩yj as '二月份',d.业绩yj as '三月份'
from table1 a,table2 b,table2 c,table2 d
where a.部门dep = b.部门dep and b.月份mon = '一月份' and
a.部门dep = c.部门dep and c.月份mon = '二月份' and
a.部门dep = d.部门dep and d.月份mon = '三月份' and
2)
select a.dep,
sum(case when b.mon=1 then b.yj else 0 end) as '1',
sum(case when b.mon=2 then b.yj else 0 end) as '2',
sum(case when b.mon=3 then b.yj else 0 end) as '3',
sum(case when b.mon=4 then b.yj else 0 end) as '4',
sum(case when b.mon=5 then b.yj else 0 end) as '5',
sum(case when b.mon=6 then b.yj else 0 end) as '6',
sum(case when b.mon=7 then b.yj else 0 end) as '7',
sum(case when b.mon=8 then b.yj else 0 end) as '8',
sum(case when b.mon=9 then b.yj else 0 end) as '9',
sum(case when b.mon=10 then b.yj else 0 end) as '10',
sum(case when b.mon=11 then b.yj else 0 end) as '11',
sum(case when b.mon=12 then b.yj else 0 end) as '12'
from (table2 a left join table1 b on a.dep=b.dep) group by a.dep

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值