SQL联合查询问题

时间:2017-02-01 15:35:33

标签: sql select union

以下代码有什么问题?我需要它在两个select语句之间进行组合。

select  
    tt.SlpName, tt.Sum, tt.Duedate 
from
    (select  
         t3.docnum as 'Invoice', null as 'Receipt', 
         isnull(t6.SlpName, t6.SlpName) 'slpname', 
         null as 'Sum', null as 'Duedate'
     from 
         oitr t0
     inner join 
         ITR1 t1 on t0.ReconNum = t1.ReconNum
     inner join 
         ojdt t2 on t1.transid = t2.transid 
     inner join
         oinv t3 on  t3.docnum = t2.BaseRef and t1.IsCredit = 'D'
     inner join 
         oslp t6 on t3.slpcode = t6.SlpCode
     where 
         t1.ReconNum = '819272'

     union all 

     select 
         t4.docnum as 'Receipt', null as 'Invoice',
         'slpname', t5.checksum 'Sum', t5.DueDate 'Duedate' 
     from 
         oitr t0
     inner join 
         ITR1 t1 on t0.ReconNum = t1.ReconNum
     inner join 
         ojdt t2 on t1.transid = t2.transid 
     inner join 
         orct t4 on t4.docnum = t2.BaseRef  and t1.IsCredit = 'C'
     inner join 
         rct1 t5 on t5.docnum = t4.DocNum
     where 
         t1.ReconNum = '819272') tt

enter image description here

enter image description here

我希望它像这样

谢谢

1 个答案:

答案 0 :(得分:0)

将底部查询中的'slpname'替换为isnull(t6.SlpName,t6.SlpName) 并添加inner JOIN oslp t6 on t3.slpcode = t6.SlpCode