2017-02-11 2 views
0

J'essaie de prévoir un montant entre deux dates. Le montant est réparti uniformément entre les dates - voir les tableaux ci-dessous pour référence. Devrait (espérons-le) être simple - des idées?SQL - Distribution des montants entre les dates

La table source ressemble à ceci:

+-----------+---------------+---------------+------+--------------+----------------+ 
| Project | Start Date | End Date | Days | Total Budget | Budget Per Day | 
+-----------+---------------+---------------+------+--------------+----------------+ 
| Project 1 | Jan. 01, 2017 | Apr. 11, 2017 | 100 |   100 | 1.00   | 
| Project 2 | Feb. 05, 2017 | Apr. 06, 2017 | 60 |   200 | 3.33   | 
| Project 3 | Feb. 03, 2017 | May. 03, 2017 | 89 |   50 | 0.56   | 
| Project 4 | Jan. 01, 2017 | Aug. 04, 2017 | 215 |   300 | 1.40   | 
+-----------+---------------+---------------+------+--------------+----------------+ 

Le tableau résultant devrait ressembler à ceci:

+-----------+---------------+--------+ 
| Project |  Day  | Budget | 
+-----------+---------------+--------+ 
| Project 1 | Jan. 01, 2017 | 1.00 | 
| Project 1 | Jan. 02, 2017 | 1.00 | 
| Project 1 | Jan. 03, 2017 | 1.00 | 
| Project 1 | Jan. 04, 2017 | 1.00 | 
| Project 1 | Jan. 05, 2017 | 1.00 | 
| …   | …    | …  | 
+-----------+---------------+--------+ 

Side note: L'objectif consiste alors à faire facilement différentes agrégations: par mois, par année, etc. Et éventuellement des distributions différentes telles que 10% du budget dans les premiers 10% des jours, etc. Toute suggestion sur la meilleure façon de le faire serait grandement appréciée.

Répondre

1

Tout d'abord, générez vous-même une table Calendrier. Ils sont extrêmement utiles!

CREATE TABLE dbo.tCalendar(
    Date_Value DATE PRIMARY KEY NOT NULL, 
    Year AS (DATEPART(YEAR, Date_Value)) PERSISTED, 
    Month AS (DATEPART(MONTH, Date_Value)) PERSISTED, 
    Day AS (DATEPART(DAY, Date_Value)) PERSISTED, 
    Day_Of_Year AS (DATEPART(DY, Date_Value)) PERSISTED, 
) 

INSERT INTO dbo.tCalendar 
SELECT * 
FROM (
    SELECT DATEADD(DAY, ROW_NUMBER() OVER(ORDER BY (SELECT 1)) - 1, '2000-01-01') AS d 
    FROM  (VALUES(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1))v(n) 
    CROSS JOIN (VALUES(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1))w(n) 
    CROSS JOIN (VALUES(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1))x(n) 
    CROSS JOIN (VALUES(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1))y(n) 
) cal 
WHERE cal.d <= '2027-12-31' 

--SELECT * FROM dbo.tCalendar 

Maintenant, vous pouvez simplement JOIN la table de calendrier pour obtenir le résultat dont vous avez besoin:

CREATE TABLE #Project(
    Project VARCHAR(100) PRIMARY KEY NOT NULL, 
    Start_Date DATE, 
    End_Date DATE, 
    Days AS (DATEDIFF(DAY, Start_Date,End_Date) + 1) PERSISTED, 
    Total_Budget DECIMAL(19,2), 
    Budget_Per_Day AS (CAST(Total_Budget/(DATEDIFF(DAY, Start_Date,End_Date) + 1) AS DECIMAL(19,2))) PERSISTED 
) 

INSERT INTO #Project (Project, Start_Date, End_Date, Total_Budget) 
VALUES 
('Project 1', '2017-01-01', '2017-04-10', 100), 
('Project 2', '2017-02-05', '2017-04-05', 200), 
('Project 3', '2017-02-03', '2017-05-02', 50), 
('Project 4', '2017-01-01', '2017-08-03', 300) 

SELECT * 
FROM #Project 

SELECT p.Project 
     ,cal.Date_Value 
     ,p.Budget_Per_Day 
FROM #Project p 
    INNER JOIN dbo.tCalendar cal 
     ON cal.Date_Value BETWEEN p.Start_Date AND p.End_Date 

Pour des distributions plus complexes, une solution est d'avoir une table pour stocker l'allocation budgétaire par une plage de dates, par exemple:

CREATE TABLE #Project_Budget_Range(
    Project VARCHAR(100) NOT NULL, 
    Range_Start_Date DATE NOT NULL, 
    Range_End_Date DATE NOT NULL, 
    Range_Days AS (DATEDIFF(DAY, Range_Start_Date, Range_End_Date) + 1) PERSISTED, 
    Budget_Allocation_Value DECIMAL(19,0) NULL, 
    Budget_Allocation_Pct DECIMAL(9,4) NULL, 
    PRIMARY KEY (Project, Range_Start_Date), 
    -- End Date >= Start Date 
    CONSTRAINT CK_Range_Start_End_Date CHECK (Range_End_Date>=Range_Start_Date), 
    -- Either Budget_Allocation_Value or Budget_Allocation_Pct must have a value (but not both) 
    CONSTRAINT CK_Allocation_Type CHECK (Budget_Allocation_Value + Budget_Allocation_Pct IS NULL AND COALESCE(Budget_Allocation_Value,Budget_Allocation_Pct) IS NOT NULL) 
) 

INSERT INTO #Project_Budget_Range 
VALUES 
('Project 1', '2017-01-01', '2017-01-31', NULL, 0.5), 
('Project 1', '2017-02-01', '2017-02-28', 30, NULL), 
('Project 1', '2017-03-01', '2017-04-10', 20, NULL) 

SELECT * FROM #Project_Budget_Range 

SELECT p.Project 
     ,p.Total_Budget 
     ,cal.Date_Value 
     ,CAST(COALESCE( 
      (b.Budget_Allocation_Pct * p.Total_Budget)/b.Range_Days, -- Allocation by Pct 
      b.Budget_Allocation_Value/b.Range_Days -- Allocation by value 
     ) AS DECIMAL(19,8)) AS Budget_Allocation 
FROM #Project p 
    INNER JOIN #Project_Budget_Range b 
     ON p.Project = b.Project 
    INNER JOIN dbo.tCalendar cal 
     ON cal.Date_Value BETWEEN b.Range_Start_Date AND Range_End_Date 
WHERE p.Project = 'Project 1' 
; 

Sample output showing budget allocation by day

+0

Merci Serge !!!!!! – FirstRedPepper

+0

Serge, si tous les projets utilisaient les mêmes critères - 10% des premiers jours utilisent 10% du budget, 50% des jours utilisent 70%, et les 40% restants utilisent les 20% restants du budget - comment ajuster le code ci-dessus? – FirstRedPepper