我有一些租赁信息,包括租赁开始的日期和可能的停止日期(如果没有停止,我们假设仍在租金中)。我也有关于这些项目的发票信息,包括该项目每张发票的计费周期的开始和停止。
示例数据:
Rental Contract | Item | Rental Start | Rental Stop | Invoice Number | Bill Start | Bill Stop
1234 |A | 4/1/2019 | | 4444 | 4/1/2019 | 4/30/2019
1234 |A | 4/1/2019 | | 4445 | 5/1/2019 | 5/31/2019
1234 |A | 4/1/2019 | | 4444 | 6/5/2019 | 6/30/2019
1234 |B | 4/1/2019 | 5/15/2019 | 4445 | 5/1/2019 | 5/15/2019
我想返回项目 A 有 6/1 - 6/4 的错过期和项目 B 的 4/1 - 4/30 的错过期。我需要做的是返回一个数据集,该数据集返回上面的行,但插入了缺失的行,以便它们可以在最终报告中突出显示。
这是我需要返回的:
Rental Contract | Item | Rental Start | Rental Stop | Invoice Number | Bill Start | Bill Stop
1234 |A | 4/1/2019 | | 4444 | 4/1/2019 | 4/30/2019
1234 |A | 4/1/2019 | | 4445 | 5/1/2019 | 5/31/2019
1234 |A | 4/1/2019 | | | 6/1/2019 | 6/4/2019
1234 |A | 4/1/2019 | | 4444 | 6/5/2019 | 4/30/2019
1234 |B | 4/1/2019 | 5/15/2019 | | 4/1/2019 | 4/30/2019
1234 |B | 4/1/2019 | 5/15/2019 | 4445 | 5/1/2019 | 5/15/2019
编辑:
所以这就是我尝试过的。我可以缩小到我知道有问题的行并查看差距。我只需要对数据进行更多处理以更改这些以显示缺少的开始和停止应该是什么,然后将其结合回原始数据。前几天我无法理解它,看到这里提供的答案帮助了我。感谢大家。
DECLARE @TicketNumber AS DECIMAL(19, 6) = 109138;
WITH rentals
AS (SELECT DTH.Rental_Ticket
--,DTH.Order_Date
--,DTH.Customer_Code
--,DTH.Customer_Name
--,DTH.Last_Invoice
,DTL.Inventory_Item_ID
--,DTL.Line_Description AS Ticket_Line_Description
--,DTL.Delivery_Date
--,DTL.Original_Start_Rent_Date
,COALESCE(DTL.Original_Start_Rent_Date, DTL.Delivery_Date, DTH.Order_Date) AS Rental_Start_Date
--,dtl.Rental_Stop_Date
--,dtl.Return_Date
,COALESCE(DTL.Rental_Stop_Date, DTL.Return_Date) AS Rental_Stop_Date
,IL.Invoice_Number
,LAG(IL.Rental_Stop_Date) OVER (PARTITION BY DTH.Rental_Ticket
,DTL.Inventory_Item_ID
ORDER BY DTH.Rental_Ticket
,DTL.Inventory_Item_ID
,IL.Billing_Cycle_Period_Start_DateTime
,IL.Rental_Stop_Date) AS Prev_Stop_Bill_Date
,IL.Billing_Cycle_Period_Start_DateTime AS Billing_Start_Date --Invoice_Billing_Start
,IL.Rental_Stop_Date AS Billing_Stop_Date --Invoice_Billing_Stop
,LEAD(IL.Rental_Stop_Date) OVER (PARTITION BY DTH.Rental_Ticket
,DTL.Inventory_Item_ID
ORDER BY DTH.Rental_Ticket
,DTL.Inventory_Item_ID
,IL.Billing_Cycle_Period_Start_DateTime
,IL.Rental_Stop_Date) AS Next_Start_Bill_Date
--,il.Billing_Logic_Itype
--,il.Line_Description AS Invoice_Line_Description
--,il.Gross_Line_Pre_Tax_Total
FROM dbo.CSView_DEL_Ticket_Header_Master AS DTH
LEFT JOIN dbo.CSView_DEL_Ticket_Lines_Master AS DTL
ON DTH.Rental_Ticket = DTL.Rental_Ticket
LEFT JOIN dbo.CSView_INVC_Lines_Master AS IL
ON DTL.Rental_Ticket = IL.Rental_Ticket_or_Tag_Number
AND DTL.Rental_Ticket_Unique_Line_ID = IL.Rental_Ticket_Unique_Line_ID_or_Tag_OrigCounter
WHERE DTH.Ticket_Type IN ('D', 'T')
AND DTH.Rental_Ticket NOT IN
(SELECT DV.dticket
FROM dbo.deltick_void AS DV)
AND IL.Billing_Logic_Itype <> 'C'
-- AND DTH.Order_Date BETWEEN '4/1/2018' AND '9/20/2018'
AND DTH.Rental_Ticket = @TicketNumber)
SELECT R.Rental_Ticket
,R.Inventory_Item_ID
,R.Rental_Start_Date
,R.Rental_Stop_Date
,R.Invoice_Number
,R.Prev_Stop_Bill_Date
,R.Billing_Start_Date
,R.Billing_Stop_Date
,R.Next_Start_Bill_Date
FROM rentals AS R
WHERE (R.Rental_Start_Date <> R.Billing_Start_Date
AND R.Prev_Stop_Bill_Date IS NULL) --first billing where there is a gap between rental start and bill start
OR (DATEADD(DAY, 1, R.Prev_Stop_Bill_Date) <> R.Billing_Start_Date) --where there is a gap between last billing stop and current billing start
ORDER BY R.Rental_Ticket
,R.Inventory_Item_ID
,R.Billing_Start_Date
,R.Billing_Stop_Date