获取连接3个表的空值

wdebmtf2  于 2021-08-09  发布在  Java
关注(0)|答案(4)|浏览(213)

下面是查询,我加入了3个表供应商是主表。实际的情况是我想要表account payable表中的所有数据,虽然它不是他们在采购订单表中的数据,所以我加入了使用full outer与供应商和采购订单,但是供应商的详细信息没有与account payable表中的数据相对应,尽管供应商键可用。

SELECT ISNULL(dbo.Supplier.supplier_key,dbo.Fact_AccountPayables.supplier_key) AS supplier_key,
                  dbo.Supplier.Supplier,
                  dbo.Supplier.Name,
                  dbo.Supplier.Status, 
                  dbo.Supplier.AddressCode, 
                  dbo.Supplier.Address,
                  dbo.Supplier.HouseNo,
                  dbo.Supplier.Street,
                  dbo.Supplier.City, 
                  dbo.Supplier.Country,
                  dbo.Supplier.ZipCode,
                  dbo.Supplier.StartDate,
                  dbo.Supplier.CreditLimit, 
                  dbo.Supplier.FinancialGroup,
                  dbo.Supplier.LastTransactionDate, 
                  dbo.Fact_PurchaseOrder.Company, 
                  ISNULL(dbo.Fact_PurchaseOrder.[Purchase Order],dbo.Fact_AccountPayables.[PO Number]) AS PurchaseOrder,
                  ISNULL( dbo.Fact_PurchaseOrder.Sequence,dbo.Fact_AccountPayables.Line) AS POSequence, 
                  dbo.Fact_PurchaseOrder.[Order Quantity], 
                  dbo.Fact_PurchaseOrder.[Per Purchase Unit], 
                  dbo.Fact_PurchaseOrder.[Per Quantity Price],
                  dbo.Fact_PurchaseOrder.[Purchase price unit],
                  dbo.Fact_PurchaseOrder.[Total Order Amount],
                  dbo.Fact_PurchaseOrder.Currency, 
                  dbo.Fact_PurchaseOrder.[Rate Date], 
                  dbo.Fact_PurchaseOrder.[Actual Receipt Date],
                  dbo.Fact_PurchaseOrder.[Receipt No],
                  dbo.Fact_PurchaseOrder.[Receipt Sequence], 
                  dbo.Fact_PurchaseOrder.[Received Quantity],
                  dbo.Fact_PurchaseOrder.[Approved Quantity], 
                  dbo.Fact_PurchaseOrder.[Purchase Office], 
                  dbo.Fact_PurchaseOrder.[Invoice Number], 
                  dbo.Fact_PurchaseOrder.[Invoice Date], 
                  dbo.Fact_PurchaseOrder.[Invoice Quantity],
                  dbo.Fact_PurchaseOrder.[Invoice Amount],
                  dbo.Fact_AccountPayables.InvoiceNumber, 
                  dbo.Fact_AccountPayables.Type AS InvoiceType, 
                  dbo.Fact_AccountPayables.[Order Type] AS OrderInvoiceType,
                  dbo.Fact_AccountPayables.AP_Balance_EUR, 
                  dbo.Fact_AccountPayables.[Invoice Amount_EUR],
                  dbo.Fact_AccountPayables.supplier_key AS EXPR2,
                  dbo.Fact_AccountPayables.[IntercompanyTrade Order No] AS EXPR23, 
                  dbo.Fact_AccountPayables.[IntercompanyTrade Line Number] AS EXPR24,
                  dbo.Fact_AccountPayables.[Intercompany Trade Financial Company] AS EXPR25, 
                  dbo.Fact_AccountPayables.[Intercompany Trade Purchase Company] AS EXPR26,
                  dbo.Fact_AccountPayables.InvoiceNumber,
                  dbo.Fact_AccountPayables.DueDate,
                  dbo.Fact_AccountPayables.DocDate, 
                  dbo.Fact_PurchaseOrder.[Order Date],
                  dbo.Fact_AccountPayables.[Invoice Amount_EUR],
                  (CASE WHEN dbo.Fact_PurchaseOrder.[Receipt No] = ' ' THEN dbo.Fact_PurchaseOrder.[Total Order Amount]
                        WHEN  dbo.Fact_PurchaseOrder.[Receipt No] != ' ' and dbo.Fact_AccountPayables.InvoiceNumber IS NULL then dbo.Fact_PurchaseOrder.[Total Order Amount] END) AS ORDERBALANCE,
                  (dbo.Supplier.CreditLimit -(ORDERBALANCE + dbo.Fact_AccountPayables.[Invoice Amount_EUR])) AS Availablecredit
    FROM            dbo.Supplier 
            LEFT OUTER JOIN dbo.Fact_PurchaseOrder ON dbo.Supplier.supplier_key = dbo.Fact_PurchaseOrder.buyfrom_supplier_key
            full OUTER JOIN dbo.Fact_AccountPayables ON dbo.Fact_AccountPayables.supplier_key = dbo.Supplier.supplier_key AND
                                                        dbo.Fact_AccountPayables.[PO Number] = dbo.Fact_PurchaseOrder.[Purchase Order]   AND 
                                                    dbo.Fact_AccountPayables.[PO Line] = dbo.Fact_PurchaseOrder.Sequence

输出如下:

vxbzzdmp

vxbzzdmp1#

在from子句之后尝试payable表,并使用left join连接其他表

FROM dbo.Fact_AccountPayables         
LEFT JOIN dbo.Fact_PurchaseOrder ON  dbo.Fact_AccountPayables.[PO Number] =  dbo.Fact_PurchaseOrder.[Purchase Order]   AND 
 dbo.Fact_AccountPayables.[PO Line] =  dbo.Fact_PurchaseOrder.Sequence
LRFT JOIN dbo.Supplier  ON dbo.Supplier.supplier_key = dbo.Fact_PurchaseOrder.buyfrom_supplier_key AND dbo.Fact_AccountPayables.supplier_key = dbo.Supplier.supplier_key
jv4diomz

jv4diomz2#

您不仅使用了供应商密钥,还使用了订单号和订单行作为完全外部联接的联接标准。如果在supplier表中没有匹配的条目(同时匹配所有联接条件的条目),您还将看到您提到的空值。
只是为了测试,暂时删除其他连接条件(订单号和行),只留下供应商密钥。检查在这种情况下是否仍然看到空值。

gtlvzcf8

gtlvzcf83#

您正在对fact\u accountpayables表使用完全联接。这样,即使与其他两个表不匹配,也会包括来自“应付帐款”的所有记录。如果您不想这样做,请使用左连接而不是完全连接。

SELECT
  ISNULL(dbo.Supplier.supplier_key, dbo.Fact_AccountPayables.supplier_key) AS supplier_key,
  dbo.Supplier.Supplier,
  dbo.Supplier.Name,
  dbo.Supplier.Status, 
  dbo.Supplier.AddressCode, 
  dbo.Supplier.Address,
  dbo.Supplier.HouseNo,
  dbo.Supplier.Street,
  dbo.Supplier.City, 
  dbo.Supplier.Country,
  dbo.Supplier.ZipCode,
  dbo.Supplier.StartDate,
  dbo.Supplier.CreditLimit, 
  dbo.Supplier.FinancialGroup,
  dbo.Supplier.LastTransactionDate, 
  dbo.Fact_PurchaseOrder.Company, 
  ISNULL(dbo.Fact_PurchaseOrder.[Purchase Order], dbo.Fact_AccountPayables.[PO Number]) AS PurchaseOrder,
  ISNULL(dbo.Fact_PurchaseOrder.Sequence, dbo.Fact_AccountPayables.Line) AS POSequence,
  dbo.Fact_PurchaseOrder.[Order Quantity], 
  dbo.Fact_PurchaseOrder.[Per Purchase Unit], 
  dbo.Fact_PurchaseOrder.[Per Quantity Price],
  dbo.Fact_PurchaseOrder.[Purchase price unit],
  dbo.Fact_PurchaseOrder.[Total Order Amount],
  dbo.Fact_PurchaseOrder.Currency, 
  dbo.Fact_PurchaseOrder.[Rate Date], 
  dbo.Fact_PurchaseOrder.[Actual Receipt Date],
  dbo.Fact_PurchaseOrder.[Receipt No],
  dbo.Fact_PurchaseOrder.[Receipt Sequence], 
  dbo.Fact_PurchaseOrder.[Received Quantity],
  dbo.Fact_PurchaseOrder.[Approved Quantity], 
  dbo.Fact_PurchaseOrder.[Purchase Office], 
  dbo.Fact_PurchaseOrder.[Invoice Number], 
  dbo.Fact_PurchaseOrder.[Invoice Date], 
  dbo.Fact_PurchaseOrder.[Invoice Quantity],
  dbo.Fact_PurchaseOrder.[Invoice Amount],
  dbo.Fact_AccountPayables.InvoiceNumber, 
  dbo.Fact_AccountPayables.Type AS InvoiceType, 
  dbo.Fact_AccountPayables.[Order Type] AS OrderInvoiceType,
  dbo.Fact_AccountPayables.AP_Balance_EUR, 
  dbo.Fact_AccountPayables.[Invoice Amount_EUR],
  dbo.Fact_AccountPayables.supplier_key AS EXPR2,
  dbo.Fact_AccountPayables.[IntercompanyTrade Order No] AS EXPR23, 
  dbo.Fact_AccountPayables.[IntercompanyTrade Line Number] AS EXPR24,
  dbo.Fact_AccountPayables.[Intercompany Trade Financial Company] AS EXPR25, 
  dbo.Fact_AccountPayables.[Intercompany Trade Purchase Company] AS EXPR26,
  dbo.Fact_AccountPayables.InvoiceNumber,
  dbo.Fact_AccountPayables.DueDate,
  dbo.Fact_AccountPayables.DocDate, 
  dbo.Fact_PurchaseOrder.[Order Date],
  dbo.Fact_AccountPayables.[Invoice Amount_EUR],
  CASE
    WHEN dbo.Fact_PurchaseOrder.[Receipt No] = ' ' THEN dbo.Fact_PurchaseOrder.[Total Order Amount]
    WHEN dbo.Fact_AccountPayables.InvoiceNumber IS NULL THEN dbo.Fact_PurchaseOrder.[Total Order Amount]
  END AS ORDERBALANCE,
  dbo.Supplier.CreditLimit - (ORDERBALANCE + dbo.Fact_AccountPayables.[Invoice Amount_EUR]) AS Availablecredit
FROM
  dbo.Supplier 
  LEFT JOIN dbo.Fact_PurchaseOrder ON dbo.Supplier.supplier_key = dbo.Fact_PurchaseOrder.buyfrom_supplier_key
  LEFT JOIN dbo.Fact_AccountPayables ON
    dbo.Fact_AccountPayables.supplier_key = dbo.Supplier.supplier_key AND
    dbo.Fact_AccountPayables.[PO Number] = dbo.Fact_PurchaseOrder.[Purchase Order]   AND 
    dbo.Fact_AccountPayables.[PO Line] = dbo.Fact_PurchaseOrder.Sequence
r7s23pms

r7s23pms4#

如果没有供应商,则在事实采购订单中筛选某些行。您需要对所有表进行完全外部联接,以获取所有应付款行的数据,而不管它们是否有供应商。

FROM  dbo.Supplier 
FULL OUTER JOIN dbo.Fact_PurchaseOrder ON dbo.Supplier.supplier_key = dbo.Fact_PurchaseOrder.buyfrom_supplier_key
FULL OUTER JOIN dbo.Fact_AccountPayables ON dbo.Fact_AccountPayables.supplier_key = dbo.Supplier.supplier_key AND
                                                        dbo.Fact_AccountPayables.[PO Number] = dbo.Fact_PurchaseOrder.[Purchase Order]   AND 
                                                    dbo.Fact_AccountPayables.[PO Line] = dbo.Fact_PurchaseOrder.Sequence

相关问题