第一个结果是这样的:
date dept count1
2017-02-20 330900 1
2017-02-21 330900 5
2017-02-22 330900 3
2017-02-23 330900 8
2017-02-24 330900 21
2017-02-26 330900 3
第二个结果是这样的:
date dept count2
2017-02-21 330900 15
2017-02-22 330900 23
2017-02-23 330900 8
2017-02-25 330900 3
2017-02-26 330900 3
想要这样的结果:
date dept count1 count2
2017-02-20 330900 1
2017-02-21 330900 5 15
2017-02-22 330900 3 23
2017-02-23 330900 8 8
2017-02-24 330900 21
2017-02-25 330900 3
2017-02-26 330900 3 3
求高手怎么合并呢?
date dept count1
2017-02-20 330900 1
2017-02-21 330900 5
2017-02-22 330900 3
2017-02-23 330900 8
2017-02-24 330900 21
2017-02-26 330900 3
第二个结果是这样的:
date dept count2
2017-02-21 330900 15
2017-02-22 330900 23
2017-02-23 330900 8
2017-02-25 330900 3
2017-02-26 330900 3
想要这样的结果:
date dept count1 count2
2017-02-20 330900 1
2017-02-21 330900 5 15
2017-02-22 330900 3 23
2017-02-23 330900 8 8
2017-02-24 330900 21
2017-02-25 330900 3
2017-02-26 330900 3 3
求高手怎么合并呢?
解决方案
2
38
select date , dept,sum(count1),sum(count2)
from (
select date , dept , count1, 0 as count2
from 第一个结果
union all
select date , dept , 0 as count1, count2
from 第二个结果
) t
group by date , dept
from (
select date , dept , count1, 0 as count2
from 第一个结果
union all
select date , dept , 0 as count1, count2
from 第二个结果
) t
group by date , dept