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

一道数据库的面试题
有A表,怎么变成B表输出
A表
编号       房间      数量        负责
1         A        20        张三
2         B        40         李四
3         A        50         王五
4         A        50          赵六
5         C        70        张三


C
1    负责   A     B         C       
2     张三  20    0         70
3     李四   0     40        0
4     王五   050    0        0



大概是这个意思,,

有没有大神说下啊


 

------解决方案--------------------
select 负责,sum(decode(房间,'A',数量,0)) as A,sum(decode(房间,'B',数量,0)) as B,sum(decode(房间,'C',数量,0)) as C from A group by 负责
------解决方案--------------------
 with a as(
 select 1 编号,'A' 房间,20 数量,'张三' 负责 from dual
 union all
 select 2,'B',40,'李四' from dual
 union all
 select 3,'A',50,'王五' from dual
 union all
 select 4,'A',50,'赵六' from dual
 union all
  select 5,'C',70,'张三' from dual
 )
 select 负责,
        sum(decode(房间, 'A', 数量, 0)) A,
        sum(decode(房间, 'B', 数量, 0)) B,
        sum(decode(房间, 'C', 数量, 0)) C
   from a
  group by 负责;
负责          A          B          C
---- ---------- ---------- ----------
王五         50          0          0
张三         20          0         70
李四          0         40          0
赵六         50          0          0