我试图计算不同资源的滚动计划状态日期。
Use AdventureWorks2012;
IF OBJECT_ID('TempTable1', 'U') IS NOT NULL
DROP TABLE TempTable1
SELECT SOH.SalesPersonID, SOD.ProductId as ResourceID,
SOH.DueDate as DueDate,
SOD.OrderQty as DayIncrement,
DATEADD(Day, SOD.OrderQty, SOH.DueDate) as SchedStartDate,
--
-- What I actually want for SchedStart date is
-- DATEADD(Day, SOD.OrderQty, SOH.DueDate) FOR THE FIRST RECORD of a Resource ID
-- Then That Date + DayIncrement for subsequent Records
--
-- The first Three Records for 707 should be
-- 2005-08-14
-- 2005-08-18
-- 2005-08-28
--
Row_Number() over (PARTITION BY SOD.ProductID ORDER By SOH.DueDate) as JResourceNumber
INTO TempTable1
FROM Sales.SalesOrderHeader SOH
INNER JOIN Sales.SalesOrderDetail SOD
ON SOH.SalesOrderID = SOD.SalesOrderID
WHERE SOH.SalesPersonID <= 274 and
SOD.ProductId < 710
ORDER by ResourceID, DueDate
Select * from TempTable1
SalesPersonID ResourceID DueDate DayIncrement SchedStartDate JResourceNumber
274 707 2005-08-13 00:00:00.000 1 2005-08-14 00:00:00.000 1
274 707 2006-06-13 00:00:00.000 4 2006-06-17 00:00:00.000 2
274 707 2006-08-13 00:00:00.000 10 2006-08-23 00:00:00.000 3