1

I am trying to create a query that runs against its own results until the results have satisfied what I am looking for. I need to write SQL that will tell me what the next row's [ProjStart] date should be based on the previous row's [ProjStart] date + the previous rows [EffortDays], per [EmpName]. The order of each task is based on the [Position] column partitioned by the [EmpName]. The trick is that not all rows will have a date until the query tells it to have the [ProjStart]+[EffortDays] of the previous row.

Below is a sample table to help create the desired results

DECLARE @aTestTable TABLE  
     (  
        Position int,  
        EmpName   VARCHAR(10),  
        ProjStart  DATE,
        EffortDays int
     );  
  
 INSERT INTO @aTestTable VALUES 
 ('1', 'Adam', '2023-05-01',2),
 ('2', 'Adam', NULL,3), 
 ('3', 'Adam', NULL,1), 
 ('4', 'Adam', NULL,2), 
 ('5', 'Adam', NULL,4), 
 ('6', 'Adam', NULL,3), 
 ('1', 'Bill', '2023-05-07',4), 
 ('2', 'Bill', NULL,5), 
 ('3', 'Bill', NULL,1), 
 ('4', 'Bill', NULL,3), 
 ('5', 'Bill', NULL,6), 
 ('1', 'Bob', '2023-05-10',4),
 ('2', 'Bob', NULL,2),
 ('3', 'Bob', NULL,5),
 ('4', 'Bob', NULL,1),
 ('5', 'Bob', NULL,1),
 ('6', 'Bob', NULL,2),
 ('7', 'Bob', NULL,3),
 ('1', 'Dave', '2023-04-28',2),
 ('2', 'Dave', NULL,1),
 ('3', 'Dave', NULL,5),
 ('4', 'Dave', NULL,4)

 SELECT * FROM @aTestTable

Please let me know if I can provide clarification.

Below is the expected output taking the previous rows [ProjStart] and adding the previous rows [EffortDays] for the current rows [ProjStart] partitioned by [EmpName] and ordered by [Position].

Position    EmpName ProjStart   EffortDays
1   Adam    2023-05-01  2
2   Adam    2023-05-03  3
3   Adam    2023-05-06  1
4   Adam    2023-05-07  2
5   Adam    2023-05-09  4
6   Adam    2023-05-13  3
1   Bill    2023-05-07  4
2   Bill    2023-05-11  5
3   Bill    2023-05-16  1
4   Bill    2023-05-17  3
5   Bill    2023-05-20  6
1   Bob 2023-05-10  4
2   Bob 2023-05-14  2
3   Bob 2023-05-16  5
4   Bob 2023-05-21  1
5   Bob 2023-05-22  1
6   Bob 2023-05-23  2
7   Bob 2023-05-25  3
1   Dave    2023-04-28  2
2   Dave    2023-04-30  1
3   Dave    2023-05-01  5
4   Dave    2023-05-06  4
5
  • 1
    Can you edit your question and add the expected output ?
    – SelVazi
    May 26 at 21:32
  • 2
    "trying to create a query" in that case please share what you tried, and explain in what way it does not meet the desired results.
    – Peter B
    May 26 at 21:41
  • If I understand you correctly you should be able to use a windowed sum of effort days from the last date provided, and add them onto the last date provided. I suggested windowed sum of effortdays and min prostart partitioned by user.
    – Dale K
    May 26 at 22:19
  • @SelVazi I have edited the original question with desired results. May 26 at 22:28
  • @PeterB I do not know what to try which is why I am asking for help. I have tried CTE and then joining back on that CTE with EmpName and Position+1, but that only gets me as far as the second row's ProjStart populated for each EmpName. I need all of the null rows populated. May 26 at 22:29

2 Answers 2

1

Here is a solution that I think will do what you need.

I used a CTE (Common Table Expression) to first retrieve the rows that have ProjStart filled in, and then recursively combine the next rows' data with the ProjStart and EffortDate of the previous set of rows to produce ProjStart for the next rows.

SQL:

DECLARE @aTestTable TABLE
(
    Position   INT,
    EmpName    VARCHAR(10),
    ProjStart  DATE,
    EffortDays INT
);
  
INSERT INTO @aTestTable VALUES
  (1, 'Adam', '2023-05-01', 2),
  (2, 'Adam', NULL, 3),
  (3, 'Adam', NULL, 1),
  (4, 'Adam', NULL, 2),
  (5, 'Adam', NULL, 4),
  (6, 'Adam', NULL, 3),
  (1, 'Bill', '2023-05-07', 4),
  (2, 'Bill', NULL, 5),
  (3, 'Bill', NULL, 1),
  (4, 'Bill', NULL, 3),
  (5, 'Bill', NULL, 6),
  (1, 'Bob', '2023-05-10', 4),
  (2, 'Bob', NULL, 2),
  (3, 'Bob', NULL, 5), 
  (4, 'Bob', NULL, 1),
  (5, 'Bob', NULL, 1),
  (6, 'Bob', NULL, 2),
  (7, 'Bob', NULL, 3),
  (1, 'Dave', '2023-04-28', 2),
  (2, 'Dave', NULL, 1),
  (3, 'Dave', NULL, 5),
  (4, 'Dave', NULL, 4)

;WITH mycte AS (
  SELECT Position, EmpName, ProjStart, EffortDays
  FROM   @aTestTable
  WHERE  ProjStart IS NOT NULL
  UNION ALL
  SELECT next.Position, next.EmpName, DATEADD(dd, prev.EffortDays, prev.ProjStart) AS ProjStart, next.EffortDays
  FROM   mycte       prev
  JOIN   @aTestTable next ON next.EmpName  = prev.EmpName
                         AND next.Position = prev.Position + 1
)
SELECT * FROM mycte
ORDER BY EmpName, Position

Results:

Position EmpName ProjStart EffortDays
1 Adam 2023-05-01 2
2 Adam 2023-05-03 3
3 Adam 2023-05-06 1
4 Adam 2023-05-07 2
5 Adam 2023-05-09 4
6 Adam 2023-05-13 3
1 Bill 2023-05-07 4
2 Bill 2023-05-11 5
3 Bill 2023-05-16 1
4 Bill 2023-05-17 3
5 Bill 2023-05-20 6
1 Bob 2023-05-10 4
2 Bob 2023-05-14 2
3 Bob 2023-05-16 5
4 Bob 2023-05-21 1
5 Bob 2023-05-22 1
6 Bob 2023-05-23 2
7 Bob 2023-05-25 3
1 Dave 2023-04-28 2
2 Dave 2023-04-30 1
3 Dave 2023-05-01 5
4 Dave 2023-05-06 4

Working demo fiddle:
http://sqlfiddle.com/#!18/1c3e5c/1/0

PS I do believe that your employees deserve the weekends off, but we'll keep that for another question ;-)

1
  • What kind of sorcery is this?! I swear I tried something very similar and it only gave me the results on the row after the date was populated, and the rest were null. But this is exactly what I was looking for. Thank you! May 26 at 22:36
1

This is an other way to do it using window functions lag(), max() and sum()

The function max() was used to get the initial ProjStart per Name.

lag() was used to get the next EffortDays.

sum() will generate a running total based on the next effortDays column, which will be utilized to generate ProjStart for the next rows.

with cte as (
       select *,
       max(ProjStart) over (partition by EmpName order by position) as init_ProjStart,
       lag(EffortDays) over (partition by EmpName order by position) as next_EffortDays
       from @aTestTable
),
cte2 as (
  select *, sum(next_EffortDays) over (partition by EmpName order by position ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as running_sum
  from cte
)
select position, EmpName, coalesce(ProjStart, DATEADD(dd, running_sum, init_ProjStart)), EffortDays
from cte2

Demo here

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.