多个到一个SQL引用,不重复任何“一个”条目

时间:2013-01-12 16:15:51

标签: sql join

所以我一直在这里闲逛一会儿,但还没有能够解决这个问题,并且最终放弃了尝试猜测方法并且正在寻求帮助。

我在数据库中有三个表。一个跟踪发票级别详细信息(客户,发票的运费,付款等)另一个跟踪订单项详细信息(订单上的项目,成本/项目,销售价格/项目等),最后一个表格包含货件EG的数据:跟踪号码,运输成本等。

所以我有:

CustomerInvoice
CustomerInvoiceDetail
Shipment

我正在尝试使用单个select语句来连接这些表,但在与货件表匹配时不会重复任何发票明细。 CustomerInvoice与CustomerInoiceDetail和Shipment都有一对多的关系,因为订单可以有多个lineitems和多个货件。

到目前为止,我有:

SELECT     
    CustomerInvoice.InvoiceCode, CustomerInvoiceDetail.ItemCode,
    CustomerInvoiceDetail.LineNum, Shipment.SourceDocument, 
    Shipment.TrackingNumber, CustomerInvoiceDetail.ActualCost, 
    CustomerInvoiceDetail.ExtActualCost, Shipment.Freight AS FreightCost,
    CustomerInvoiceDetail.SalesPrice, CustomerInvoiceDetail.ExtPrice, 
    CustomerInvoiceDetail.SalesTaxAmount, CustomerInvoice.Freight, 
    CustomerInvoice.FreightTax

FROM         
    CustomerInvoiceDetail LEFT OUTER JOIN
                  Shipment RIGHT OUTER JOIN
                  CustomerInvoice 
    ON Shipment.SourceDocument = CustomerInvoice.SourceInvoiceCode 
    ON CustomerInvoiceDetail.InvoiceCode = CustomerInvoice.InvoiceCode
;

理想情况下,我们需要结果显示包含货件和订单级别详细信息的行,而不显示发票行项目详细信息,然后显示下面的所有发票行项目详细信息。我想这可以通过内部加入发票来装运,但后来不知道如何进一步包括发票明细中的每一行而不加入任何标准的其他数据。最后,我们需要避免任何订单的最终结果多次包含相同的货币数字。

作为参考我重新编写了Gordon Linoff提交的完整语法查询并运行它。奇怪的是,目前只有case语句存在问题。这就是它的样子:

`--REFERENCE JOIN STATEMENT          
SELECT CustomerInvoice.InvoiceCode, NULL as ItemCode,
       NULL as LineNum, Shipment.SourceDocument, 
       Shipment.TrackingNumber, NULL as ActualCost, 
       NULL as ExtActualCost, Shipment.Freight AS FreightCost,
       NULL as SalesPrice, NULL as ExtPrice, 
       NULL as SalesTaxAmount, CustomerInvoice.Freight, 
       CustomerInvoice.FreightTax
FROM Shipment RIGHT OUTER JOIN
     CustomerInvoice
    ON Shipment.SourceDocument = CustomerInvoice.SourceInvoiceCode 
union all
select CustomerInvoice.InvoiceCode, CustomerInvoiceDetail.ItemCode,
       CustomerInvoiceDetail.LineNum, NULL as SourceDocument, 
       NULL as TrackingNumber, CustomerInvoiceDetail.ActualCost, 
       CustomerInvoiceDetail.ExtActualCost, NULL AS FreightCost,
       CustomerInvoiceDetail.SalesPrice, CustomerInvoiceDetail.ExtPrice, 
       CustomerInvoiceDetail.SalesTaxAmount, CustomerInvoice.Freight, 
       CustomerInvoice.FreightTax
from CustomerInvoice join
     CustomerInvoiceDetail
     on CustomerInvoice.InvoiceCode = CustomerInvoiceDetail.InvoiceCode
order by InvoiceCode, ItemCode, (case when LineNum is null then 0 else 1 end), LineNum

_`

1 个答案:

答案 0 :(得分:0)

这种类型的查询在不同行上有不同的信息,通常会建议union。以下是您可能需要的示例:

SELECT ci.InvoiceCode, ci.ItemCode,
       NULL as LineNum, S.SourceDocument, 
       s.TrackingNumber, NULL as ActualCost, 
       NULL as ExtActualCost, s.Freight AS FreightCost,
       NULL as SalesPrice, NULL as ExtPrice, 
       NULL as SalesTaxAmount, ci.Freight, 
       ci.FreightTax
FROM Shipment s RIGHT OUTER JOIN
     CustomerInvoice ci
    ON s.SourceDocument = ci.SourceInvoiceCode 
union all
select ci.InvoiceCode, ci.ItemCode,
       cid.LineNum, NULL as SourceDocument, 
       NULL as TrackingNumber, cid.ActualCost, 
       cid.ExtActualCost, NULL AS FreightCost,
       cid.SalesPrice, cid.ExtPrice, 
       cid.SalesTaxAmount, ci.Freight, 
       ci.FreightTax
from CustomerInvoice ci join
     CustomerInvoiceDetail cid
     on ci.InvoiceCode = cid.InvoiceCode
order by InvoiceCode, ItemCode, (case when lineNum is null then 0 else 1 end), LineNum

case中的order by语句只是为了确保具有NULL LineNum的行首先出现。

注意:这是未经测试的SQL,因此可能存在语法错误,尤其是别名。