我试图显示所有销售订单,如果有订单,则显示交货单。我有一个查询,显示所有订单,但是无论我如何将其加入到下表中,我都将继续仅获取带有交货单的订单的结果。如果没有记录,我希望NULL
出现在交货单上。
我尝试过左,右和内联接,它们都返回相同数量的结果,这使我感到困惑。我以为我对今天的加入有了了解。
此部分显示存在多少销售订单。我希望所有结果都显示在下一个查询中
select t0.docnum SalesOrder, t1.itemcode, t1.linenum rdr1line
from ordr t0 inner join rdr1 t1 on t1.docentry = t0.docentry
where t0.CANCELED = 'N';
此部分显示带有送货单的销售订单数量,但不包括没有送货单的订单。
select t0.docnum SalesOrder, t1.itemcode, t2.docnum DelivNoteNum, t3.baseline inv1base, t1.linenum rdr1line, t3.linenum dln1line
from ordr t0 inner join rdr1 t1 on t1.docentry = t0.docentry
right outer join odln t2 on t2.docentry = t1.trgetentry
right outer join dln1 t3 on t3.docentry = t2.docentry and t3.baseline = t1.linenum and t3.ItemCode = t1.ItemCode
where t0.CANCELED = 'N' and t2.canceled = 'N';
预期结果应为每个表中的行数相同。实际结果仅与交货单一起订购。
答案 0 :(得分:0)
使用left join
,而不是right join
。如果要所有订单,则从该表开始。后续表格的过滤器应放入on
子句:
select t0.docnum SalesOrder, t1.itemcode, t2.docnum DelivNoteNum,
t3.baseline inv1base, t1.linenum rdr1line, t3.linenum dln1line
from ordr t0 left join
rdr1 t1
on t1.docentry = t0.docentry left join
odln t2
on t2.docentry = t1.trgetentry and t2.canceled = 'N' left join
dln1 t3
on t3.docentry = t2.docentry and
t3.baseline = t1.linenum and
t3.ItemCode = t1.ItemCode
where t0.CANCELED = 'N';