我想实现以下查询。 我想要的是比较销售中的发票金额,以便我需要的字段是:
OSLP.SalesPerson|OINV.CardName|OINV.DocDate|OINV.DocNum|OINV.DocTotal|ORDR.DocDate|ORDR.DocTotal|OINV.GrosProfit
到目前为止,我所做的就是这个,
Declare @mindate date = '10.01.16', @maxdate date = '10.31.16'
SELECT DISTINCT
T6.SlpName [Sales Person],
T5.CardName [Customer Name],
T5.DocDate [TRA Date],
T5.DocNum [TRA No],
T5.DocTotal [TRA Total],
(SELECT SUM(T8.DocTotal) FROM ORIN T8 INNER JOIN RIN1 T9 ON T8.DocEntry=T9.DocEntry WHERE T8.DocDate BETWEEN @mindate AND @maxdate) [Credit Amnt],
T1.DocDate [SO Date],
T1.DocTotal [SO Total],
T5.GrosProfit [Gross Profit]
FROM RDR1 T0
INNER JOIN ORDR T1 ON T0.DocEntry = T1.DocEntry
left outer JOIN DLN1 T2 on T2.BaseEntry = T0.DocEntry
left outer JOIN ODLN T3 on T2.DocEntry = T3.DocEntry
left Outer JOIN INV1 T4 on T4.BaseEntry = T3.DocEntry AND T4.BaseLine = T2.Linenum AND T4.BaseType = 15
OR (T4.Basetype=17 and T4.BaseEntry=T0.DocEntry AND T4.BaseLine=T0.LineNum)
left outer JOIN OINV T5 on T5.DocEntry = T4.DocEntry
left outer JOIN OSLP T6 on T6.SlpCode = T1.SlpCode
WHERE T5.DocDate BETWEEN @mindate AND @maxdate
Group by
T6.SlpName, T5.CardName, T1.CardCode,
T5.DocDate, T5.DocNum, T5.DocTotal,
T1.DocDate, T1.DocTotal, T5.GrosProfit
ORDER BY
T5.CardName, T5.DocDate
正如你所见,我包括了ORIN(贷项凭证)表,用ORIN.DocTotal扣除了OINV.DocTotal的和。
所以,这个查询可以正常运行,因为它假设是,但是我无法获得那些没有销售订单或交货等相关文件的发票。只是,它只是发票。