每一个SQL语句要返回相同的列(列不足的可以用常数补满),而且合并列类型要相互兼容 然后用UNION 进行合并就可以了
比如:
select 4 from dual union select 5 from dual;
         4
----------
         4
         5
合并两个集合

解决方案 »

  1.   

    如上所诉,使用union,如果相同的结果不用合并,则用union all
      

  2.   

    谢谢两位 , 不过 union &union all都只能 对 返回相同的列起作用 ,我要的是相同列合并一条,不相同列就列出来, 而不是 对 相同结果 (行)合并!
    note1的结果
    receipt_money    dept_id
    10000            8
    20000            9
    30000            7
    note2的结果
    issue_money       dept_id
    50000            11
    60000            12
    70000            13那我合并后的结果想得到  
    receipt_money     issue_money   dept_id
    10000                  0          8
    20000                  0          9
    30000                  0          7
    0                      50000      11
    0                      60000      12
    0                      70000      13
      

  3.   

    每一个SQL语句要返回相同的列(列不足的可以用常数补满)用常量补满啊!!按你现在的就用"0"来补
    note1
    select sum(round(t.qty*t.unit_price,2)) as receipt_money,0 as  issue_money,t.dept_id from mm_operation_history t 
    where t.bill_type in (1,14) 
    --and t.operation_date>= to_date('2006-09', 'yyyy-mm')
    --and t.operation_date<= add_months(to_date('2006-09', 'yyyy-mm'), 1) - 1 
    group by t.dept_id;
    --note2
    select 0 as receipt_money,sum(round(t.qty*t.unit_price,2)) as issue_money,t.dept_id from mm_operation_history t
    where t.bill_type = 2 
    group by t.dept_id;
      

  4.   

    select sum(round(t.qty*t.unit_price,2)) as receipt_money,0 as  issue_money,t.dept_id from mm_operation_history t 
    where t.bill_type in (1,14) 
    group by t.dept_id
    union
    select 0 as receipt_money,sum(round(t.qty*t.unit_price,2)) as issue_money,t.dept_id from mm_operation_history t
    where t.bill_type = 2 
    group by t.dept_id;
    你这样执行看看!!
      

  5.   

    select 1 結果︰
    A  1
    B  2
    select 2 結果︰
    A  1
    B  1union :
    A  1
    B  2
    B  1union all :
    A  1
    B  2
    A  1
    B  1minus:
    B  2intersect:
    A  1
      

  6.   

    看来我的文字表达能力欠缺了些 
    不知道这样能否看懂  
    select 1 結果︰
    column1    dept_id
    any1            1
    any1            2
    select 2 結果︰
    column2    dept_id
    any2             1
    any2             1
    select 2 結果︰
    column3    dept_id
    any3             1
    any3             1能不能得到 
    column1   column2    column3   dept_id
    any1       any2        any3      1
    any1        0           0        2
      

  7.   


    你的dept_id来对应
    try:select nvl(a.column1,0),nvl(b.column2,0),nvl(c.column3,0),d.dept_id 
    from tb_dept d,tb1 a,tb2 b,tb3 c
    where d.dept_id=a.dept_id(+) 
    and d.dept_id=b.dept_id(+) 
    and d.dept_id=c.dept_id(+);
      

  8.   

    select 
    (select sum(round(t.qty*t.unit_price,2)) from mm_operation_history t 
    where t.bill_type in (1,14) and t.dept_id=m.dept_id ) as oneMoney,
    (select sum(round(t.qty*t.unit_price,2)) from mm_operation_history t
    where t.bill_type = 2 and t.dept_id=m.dept_id ) as twoMoney,
    (select sum(round(t.qty*t.unit_price,2)) from mm_operation_history t 
    where t.bill_type = 3 and t.dept_id=m.dept_id) as threeMoney,
    (select sum(round(t.qty*t.unit_price,2)) from mm_operation_history t 
    where t.bill_type=6 and t.dept_id=m.dept_id) as fourMoney,
    (select sum(round(t.qty*t.unit_price,2)) from mm_operation_history t 
    where t.bill_type=5 and t.dept_id=m.dept_id) as fiveMoney,
    ( select sum(round(t.qty*t.unit_price,2)) from mm_operation_history t where t.operation_history_id in(
    select max(t.operation_history_id)  from mm_operation_history t 
    group by t.material_id, t.project_id ) and t.dept_id=m.dept_id) as sixMoney,m.dept_id
    from mm_operation_history m  
    where m.operation_date>= to_date('2006-09', 'yyyy-mm')
    and m.operation_date<= add_months(to_date('2006-09', 'yyyy-mm'), 1) - 1 
    group by m.dept_id
    重新拼了下,似乎得到了想要的结果,但是 性能真让人汗颜, 竟然要 6.325s
    谁能给优化下或者给出更好的解决方法, 不盛感激