所以我在这里闲逛了一段时间,但还没有弄清楚这一点,最终放弃了尝试猜测方法并寻求帮助。
我在数据库中有三个表。一个跟踪发票级别的详细信息(客户、发票的运费、付款等),另一个跟踪行项目详细信息(订单上的项目、成本/项目、售价/项目等),最后一个表包含有关货物的数据 EG :跟踪号码,运输成本等。
所以我有:
CustomerInvoice
CustomerInvoiceDetail
Shipment
我试图以一个连接这些表的选择语句结束,但在与发货表匹配时不重复任何发票详细信息。CustomerInvoice 与 CustomerInoiceDetail 和 Shipment 具有一对多的关系,因为一个订单可以有多个 lineitem 和多个发货。
到目前为止,我有:
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 提交的查询并运行它。奇怪的是,目前只有案例陈述存在问题。这是它的样子:
`--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
_`