您知道是否有办法验证SQL查询中的上一条记录?我有以下信息:
我希望看到以下信息:
这是查询:
Select
T1.Company, T1.Calc_CurDueDate, T1.InvoiceNum, t2.PackNum, T1.Calc_CustID,
0 As Calc_AgeInvAmt1,
CASE
WHEN T3.InvoiceAmt = T3.InvoiceBal THEN (SUM(T2.ExtPrice)-SUM(T2.DocDiscount))
ELSE 36600 END as Calc_AgeInvAmt2,
0 as Calc_AgeInvAmt3,
0 as Calc_AgeInvAmt4,
0 as Calc_AgeInvAmt5,
0 as Calc_AgeInvAmt6
FROM ARDtls_23b33e365b2d46cc9552a2ee324d2cb1 T1
INNER JOIN InvcDtl_23b33e365b2d46cc9552a2ee324d2cb1 T2
ON T2.Company = T2.Company AND T1.InvoiceNum = T2.InvoiceNum
INNER joIN InvcHead_23B33E365B2D46CC9552A2EE324D2CB1 T3
ON T1.Company = T3.Company AND T1.InvoiceNum = T3.InvoiceNum
Group BY T1.Company, T1.Calc_CurDueDate, T1.InvoiceNum, t2.PackNum, T1.Calc_CustID, T3.InvoiceAmt, T3.InvoiceBal
HAVING T1.Calc_CurDueDate >= @MinAgeDate AND T1.Calc_CurDueDate < @AgeDate
任何想法都会受到赞赏。
由于
答案 0 :(得分:0)
据我了解您的问题,您需要在PackNum订购时获取Calc_AgeInvAmt2列的当前行和上一行之间的差异。然后你可以使用它:
With q as (... put your query here ...)
select InvoiceNum, PackNum, CalcCustID, Calc_AgeInvAmt1, Calc_AgeInvAmt2 - coalesce(lag(Calc_AgeInvAmt2) over (order by PackNum),0) as Calc_AgeInvAmt2 from q;