TSQL滚动计划(非SUM)

时间:2016-02-26 22:48:28

标签: sql sql-server tsql

我试图计算不同资源的滚动计划状态日期。

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

1 个答案:

答案 0 :(得分:0)

如果我正确理解了这个问题你想要这个:

请注意我使用的是AdventureWorks2014,因为我没有2012。

insertNewObjectForEntityForName:inManagedObjectContext:

结果:

enter image description here