二、操作题(用SQL或Oracle)
1.某公司的员工工资表和部门情况表的表结构如下:
员工号 姓名 工资 工资月份 部门号
         表A 员工工资表
部门号 部门名称 部门经理
       表B 部门情况表
现以200604一个月的工资情况为例,用查询语句完成下列问题:
1)找出当月工资最高的员工的员工号、姓名、部门名称和工资;
2)找出当月工资最高的员工所在的部门的所有员工的平均工资;
3)找出公司各部门的部门号、部门名称和其员工当月的平均工资。
2.表C中只有一列COL001,每条记录由字符构成。现在知道每条记录中字符内容由8项组成,每项之间用‘;’隔开,8项之中有的项可能没有。试将COL001分成8列生成一张新表,原表中的每项为新表中的一列。
        COL001
86130004;000100971;01060;;
8613000219;000100122;00110;8613000219
8613000310;000100122;0AAAA;;1013089;214
          表C片断
1)画出你的解决方法的流程图、
2)写主要的代码

解决方案 »

  1.   

    1) 没有什么流程图可画, 这些都是基本的SQL语句。
    2)
    1. select t1.员工号, t1.姓名 , t2.部门名称, t1.工资 from 表A t1, 表B t2 where t1.员工号 in (select max(工资) from 表A) and t1.部门号 = t2.部门号2. select t2.部门号, avg(t1.工资) 平均工资 from 表A t1, 表B t2 where t1.员工号 in (select max(工资) from 表A) and t1.部门号 = t2.部门号 group by t1.部门号3. select b.部门号, b.部门名称, avg(a.平均工资) 平均工资 from (select 部门号, avg(工资) 平均工资 from 表A group by 部门号) a, 表B b where a.部门号 = b.部门号4. create table 表C (...);
    insert into 表C select substr('8613000219;000100122;00110;8613000219' || ';',1,instr('8613000219;000100122;00110;8613000219' || ';',';',1,1) - 1) a,
           substr('8613000219;000100122;00110;8613000219' || ';',instr('8613000219;000100122;00110;8613000219' || ';',';',1,1) + 1, instr('8613000219;000100122;00110;8613000219' || ';',';',1,2) - instr('8613000219;000100122;00110;8613000219' || ';',';',1,1) -1) b,
           substr('8613000219;000100122;00110;8613000219' || ';',instr('8613000219;000100122;00110;8613000219' || ';',';',1,2) + 1, instr('8613000219;000100122;00110;8613000219' || ';',';',1,3) - instr('8613000219;000100122;00110;8613000219' || ';',';',1,2) -1) c,
           substr('8613000219;000100122;00110;8613000219' || ';',instr('8613000219;000100122;00110;8613000219' || ';',';',1,3) + 1, instr('8613000219;000100122;00110;8613000219' || ';',';',1,4) - instr('8613000219;000100122;00110;8613000219' || ';',';',1,3) -1) d,
           substr('8613000219;000100122;00110;8613000219' || ';',instr('8613000219;000100122;00110;8613000219' || ';',';',1,4) + 1, instr('8613000219;000100122;00110;8613000219' || ';',';',1,5) - instr('8613000219;000100122;00110;8613000219' || ';',';',1,4) -1) e,
           substr('8613000219;000100122;00110;8613000219' || ';',instr('8613000219;000100122;00110;8613000219' || ';',';',1,5) + 1, instr('8613000219;000100122;00110;8613000219' || ';',';',1,6) - instr('8613000219;000100122;00110;8613000219' || ';',';',1,5) -1) f,
           substr('8613000219;000100122;00110;8613000219' || ';',instr('8613000219;000100122;00110;8613000219' || ';',';',1,6) + 1, instr('8613000219;000100122;00110;8613000219' || ';',';',1,7) - instr('8613000219;000100122;00110;8613000219' || ';',';',1,6) -1) g,
           substr('8613000219;000100122;00110;8613000219' || ';',instr('8613000219;000100122;00110;8613000219' || ';',';',1,7) + 1, instr('8613000219;000100122;00110;8613000219' || ';',';',1,8) - instr('8613000219;000100122;00110;8613000219' || ';',';',1,7) -1) h from dual另外, 这不是什么非常查询, 都是一些基本SQL。
      

  2.   

    netstring,你写的语句怎么都报错啊?运行不了啊。能否帮忙改改。谢谢。
      

  3.   

    如果COL001固定为8段,那么直接用substr就可以了如果COL001段数不固定,那么就写一个f_split函数,然后循环处理。
      

  4.   

    1) 
    select t1.员工号, t1.姓名 , t2.部门名称, t1.工资 from (select 员工号,姓名,工资,部门号 from 表A where 月份=to_char(sysdate,'MM')) t1,表B t2 where t1.工资=max(t1.工资) and ti.部门号=t2.部门号 
    2)
    select avg(工资) 平均工资 from (select 员工号,姓名,工资,部门号 from 表A where 月份=to_char(sysdate,'MM') t1 where 部门 in (select 部门 from t1 where 工资=max(t1.工资));
    3)select t1.部门号,t2.部门名称,平均工资 from (select 部门号,avg(工资) 平均工资 from (select 工资,部门号 from 表A where 月份=to_char(sysdate,'MM'))) t1,表2 t2 where t1.部门号=t2.部门号下面那个 不知道了```
      heyixiang(子豚の愛人)
         能不能举个例子说明一下F_SPLIT函数的用法啊````谢谢啦``
        
      

  5.   

    f_split函数示例
    http://blog.csdn.net/heyixiang/archive/2005/11/12/527973.aspx