当前位置: 代码迷 >> Oracle技术 >> 求一条SQL解决思路
  详细解决方案

求一条SQL解决思路

热度:30   发布时间:2016-04-24 08:32:37.0
求一条SQL
表 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  
   
 
收藏0 邀请 . 
  


------解决方案--------------------
这时间 应该是取的入库和出库时间合并吧? 

SQL code
select a.公司 ,nvl(sum(b.入库数),0) 入库数,nvl(sum(a.出库数),0) 出库数,t1.时间from(select 时间 from a union select 时间 from b) t1left joina on t1.时间=a.时间left join b on t1.时间=b.时间where a.公司=b.公司group by a.公司,t1.时间order by a.公司,t1.时间 desc
------解决方案--------------------
SQL code
select 公司,       sum(入库数),       sum(出库数),       时间  from (        select a.公司     as  公司,                0         as  入库数,               decode (a.是否出库, '1', 1, 0)                         as  出库数,               a.时间     as  时间            from a        union all        select b.公司     as  公司,                decode (b.是否入库, '1', 1, 0)                         as  入库数,               0         as  出库数,               b.时间     as  时间            from b        )group by 公司,时间order by 公司,时间
  相关解决方案