Во-первых, сгенерируйте для себя таблицу календаря. Они чрезвычайно полезны!
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
Теперь вы можете просто JOIN
календарь таблицы, чтобы получить результат, вам требуется:
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
Для более сложных распределений, одно решение иметь таблицу для хранения распределения бюджета по диапазону дат, например:
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'
;
Спасибо Serge !!!!!! – FirstRedPepper
Серж, если все проекты используют одни и те же критерии - первые 10% дней используют 10% бюджета, а следующие 50% дней используют 70%, а оставшиеся 40% дней используют оставшиеся 20% бюджета - как бы вы настраивали код выше? – FirstRedPepper