下面是三张表的数据:test1,test2,dept
test1:
month_no type   source DATE_TIME          user   DEPT_CODE money
110101 0 1 2010/4/1 17:17 张三 2 500
110102 0 1 2010/4/2 17:17 张三 2 -100
110103 0 1 2010/4/13 11:02 刘金 6 20
110104 0 0 2010/4/22 10:31 斯一 4 140
110105 1 1 2010/4/12 10:34 李四 2         50
110106 0 1 2010/4/22 10:31 赵六 2 320
/* month_no:月单编号,type:1表示饮料(中蓝药)、0表示五金(西中药),source: 1表示批发(住院)、0表示零售(门诊),date_time:签单时间,user:签单人,dept_code:签单部门, money每单费用
*/
test2:
month_no name       comp     spec    quantity     units  paymoney         
110101 原子锁 金点公司   大号       2          把     100
110101 水箱 日丰公司   10V       2          个     180
110101 热水器 万和公司   8L        1          个     220
110102 原子锁 金点公司   大号      -1         把     -50
110102 原子锁 万和公司   中号      -1         把     -50
110103 风扇  黄和   33cm      1          根     20
110104 龙头 一公司   双头      3          个     100
110104 开关 二公司   单        2          个     40
110105 牛奶  伊利   5L        1          箱     50
110106 原子锁 金点公司   大号     2          把     100
110106 热水器 万和公司   8L        1          个     220/* month_no:月单编号,spec规格,quantity数量,paymoney发生的费用 */ dept表:
DEPT_CODE  DEPT_NAME
  2           二门市部
  5           五门市部
  3           三门市部
  4           四门市部
  6           六门市部
要统计查询的是4月时间段内二门市部的 五金 批发 签单人的统计数据,正确结果应为如下形式:
name     comp     spec     sum(quantity)   units   user_name   dept_name
原子锁   金点公司  大号       1            把      张三        二门市部
原子锁   金点公司  大号       2            把      赵六        二门市部
原子锁   万和公司  中号       -1           把      张三        二门市部
水箱     日丰公司   10V       2            个      张三        二门市部
热水器 万和公司   8L       1            个       张三        二门市部
热水器 万和公司   8L       1            个       赵六        二门市部请写出select,group查询语句,在此深表感谢!

解决方案 »

  1.   

    create table test1(month_no number,type number,source number,date_time date,tuser varchar2(30),dept_code number,money number)
    insert into test1(month_no ,type ,source ,date_time ,tuser ,dept_code ,money )
    select 110101, 0, 1, to_date('2010/4/1','yyyy/mm/dd'),'张三', 2, 500 from dual 
    union 
    select 110102, 0, 1, to_date('2010/4/2','yyyy/mm/dd'),'张三', 2, -100 from dual 
    union 
    select 110103, 0, 1, to_date('2010/4/13','yyyy/mm/dd'),'刘金', 6, 20 from dual 
    union 
    select 110104, 0, 0, to_date('2010/4/22','yyyy/mm/dd'),'斯一', 4, 140 from dual 
    union 
    select 110105, 1, 1, to_date('2010/4/12','yyyy/mm/dd'),'李四', 2, 50 from dual 
    union 
    select 110106, 0, 1, to_date('2010/4/22','yyyy/mm/dd'),'赵六', 2, 320 from dual 
    commit;
    create table test2(month_no number,name varchar2(30),comp varchar2(30),spec varchar2(30),quantity number,units varchar2(10),paymoney number)
    insert into test2(month_no ,name,comp,spec,quantity ,units ,paymoney )
    select 110101 ,'原子锁', '金点公司', '大号', 2, '把', 100   from dual 
    union
    select 110101 ,'水箱', '日丰公司', '10V', 2, '个', 180   from dual 
    union
    select 110101 ,'热水器', '万和公司', '8L', 1, '个', 220   from dual 
    union
    select 110102 ,'原子锁', '金点公司', '大号', -1, '把', -50   from dual 
    union
    select 110102 ,'原子锁', '万和公司', '中号', -1, '把', -50   from dual 
    union
    select 110103 ,'风扇', '黄和', '33', 1, '根', 20   from dual 
    union
    select 110104 ,'龙头', '一公司', '双头', 3, '个', 100   from dual 
    union
    select 110104 ,'开关', '二公司', '单', 2, '个', 40   from dual 
    union
    select 110105 ,'牛奶', '伊利', '5L', 1, '箱', 50   from dual 
    union
    select 110106 ,'原子锁', '金点公司', '大号', 2, '把', 100   from dual 
    union
    select 110106 ,'热水器', '万和公司', '8L', 1, '个', 220   from dual 
    commit;create table dept (dept_code number,dept_name varchar2(30))
    insert into dept(dept_code,dept_name)
    select 2,'二门市部' from dual 
    union
    select 5,'五门市部' from dual 
    union
    select 3,'三门市部' from dual 
    union
    select 4,'四门市部' from dual 
    union
    select 6,'六门市部' from dual 
    commit;select t2.name,t2.comp,t2.spec,sum(quantity),t1.tuser,t3.dept_name from
    test1 t1 inner join test2 t2 on t1.month_no = t2.month_no
    inner join dept t3 on t1.dept_code = t3.dept_code
    where t1. type = 0 and t1.source = 1 and t1.date_time >= to_date('2010-04-01 00:00:00','yyyy-mm-dd hh24:mi:ss')
    and  t1.date_time < to_date('2010-05-01 00:00:00','yyyy-mm-dd hh24:mi:ss') and t3.dept_name = '二门市部'
    group by t2.name,t2.comp,t2.spec,t1.tuser,t3.dept_name
      

  2.   


    select * from 
    (select a.month_no,a.type,a.DATE_TIME,a.user,a.dept_code,b.name,b.comp.b.spec.b.quantity,c.DEPT_NAME
    from  test1 a,test 2, dept c
    where a.month_no=b.month_no
    and   a.dept_code=c.dept_code)
    where date_time >=to_date('2010-04-01 00:00:00','yyyy-mm-dd hh24:mi:ss')  and  t1.date_time  < to_date('2010-05-01 00:00:00','yyyy-mm-dd hh24:mi:ss')
    group by dept_code,type,user