简体   繁体   中英

SQL union query issue

What's wrong with the below code? I need it to combine between both select statements.

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

I want it to be like this

Thank you

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM