表 a   
id 公司 出库数 时间
1    A    5    3-8
2    A    5    3-7
3    A    5    3-7表 b
id 公司 入库数 时间
1   A     5      3-8
1   A     10     3-8要查询结果是
公司 入库数 出库数 时间
A     15     5     3-8
A      0     10    3-7   

解决方案 »

  1.   

    select a.公司,入库数, 出库数,a.时间 from a,b where a.id=b.id
      

  2.   


    select a.公司 ,nvl(sum(b.入库数),0) 入库数,nvl(sum(a.出库数),0) 出库数,a.时间
    from a,b
    group by a.公司,a.时间
    order by a.公司,a.时间 desc
      

  3.   

    掉了条件select a.公司 ,nvl(sum(b.入库数),0) 入库数,nvl(sum(a.出库数),0) 出库数,a.时间
    from a,b
    where a.公司=b.公司
    group by a.公司,a.时间
    order by a.公司,a.时间 desc
      

  4.   

    貌似不大对  出库时间和入库时间是分开的 没什么关系
    select a.公司 ,nvl(sum(b.入库数),0) 入库数,nvl(sum(a.出库数),0) 出库数,t1.时间
    from
    (select 时间 from a
     union 
    select 时间 from b) t1
    left join
    a on t1.时间=a.时间
    left join 
    b on t1.时间=b.时间
    where a.公司=b.公司
    group by a.公司,t1.时间
    order by a.公司,t1.时间 desc
      

  5.   


    select 公司,
           sum(入库数),
           sum(出库数),
           时间
      from (
            select a.公司   as  公司, 
                   0       as  入库数,
                   a.出库数 as  出库数,
                   a.时间   as  时间
                from a
            union all
            select b.公司   as  公司, 
                   b.入库数 as  入库数,
                   0       as  出库数,
                   b.时间   as  时间
                from b
            )
    group by 公司,时间
    order by 公司,时间
      

  6.   

    先按公司,日期分组得ta,tb
    ta,tb做全外连接……
    select ta.公司,tb.公司,nvl(tb.入库数,0),ta.出库数,ta.时间,tb.时间
    from
    (select 公司,sum(出库数) 出库数,时间 from a group by 公司,时间) ta full outer join
    (select 公司,sum(入库数) 入库数,时间 from b group by 公司,时间 )tb 
    on ta.公司=tb.公司 and ta.时间=tb.时间