有三个表结构如下:op_log(日志表) 表结构
USERID VARCHAR2(50)
USERNAME VARCHAR2(100)op_access(用户表)  表结构
USERID VARCHAR2(30)
DEPARTMENT VARCHAR2(30)dept_c(部门表) 表结构
DEP_NO VARCHAR2(30)
DEPARTMENT VARCHAR2(30)说明:
1,op_log表中的userid和op_access表中的userid对应
2,op_access表中的DEPARTMENT和dept_c表中的DEP_NO对应
3,op_log表中的username的值为'移动用户'或者非移动用户即WEB方式(要么等于'移动用户',要么不等于)要求:
通过SQL语句产生如下结果:(Oracle8i)          WEB方式 移动方式
部门名称1 统计值 统计值
部门名称2 统计值 统计值
部门名称3 统计值 统计值
...          统计值 统计值
部门名称n 统计值 统计值其中的部门名称是dept_c表中的字段DEPARTMENT

解决方案 »

  1.   

    As YOu didn't explain which table is the transaction , which table is the master table , I cannot answer you . OK , anyway I image op_log,dept_c is the master table . and op_access is the transaction table .SO the sql will be like that :select dept_c.DEPARTMENT,count(*),
           sum(decode(op_log.user_name,'WEB方式',1,0)) WEB方式,
           sum(decode(op_log.user_name,'移动方式',1,0)) 移动方式
    from op_access,dept_c,op_log
    where op_access.DEPARTMENT and dept_c.DEP_NO
          op_access.USERID = op_log.USERID 
    group by dept_c.DEPARTMENTIt is just for your reference , I believe you can get some idea in the above sql (especially the usage of decode function ) and be able to write your own perfect query .
      

  2.   

    i am learning oracle,but i don't understand the jungle of the oracle.please the master of the second floor write what you means in chinese.can you do me a favor?
    thank you very much.up! 
      

  3.   

    select  d.department as dept, d.count1 as web,e.count1 as mobile, d.count1+e.count1 as count  from
    ( select c.department,count(a.username) count1,a.username from op_log a,op_access b,dept_c c where a.userid = b.userid and c.dept_no = b.department group by c.department,a.username ) d 
    ,(select c.department,count(a.username) count1,a.username from op_log a,op_access b,dept_c c where a.userid = b.userid and c.dept_no = b.department group by c.department,a.username ) e  
    where d.username = 'web' and e.username = 'mobile' and d.department = e.departmentd 和e 分别为相同部门的web 和mobile方式的总量.black_snail(●男人要忍○) 的SQL是把web 和mobile先翻译为01 之后再对他们进行统计.
      

  4.   

    问题已经解决,可见如下:
    select c.department,count(a.userid),sum(decode(a.username,'移动用户',1,0)),sum(decode(a.username,移动用户'',0,1)) 
    from op_log a,op_access b,dept_c c
    where a.userid=b.userid and b.department=c.dept_no
    group by c.department;感谢大家,结贴!