0
votes

First time poster here and self-taught SQLer, so I know I suck (I just need it to work)

Basically, I am trying to write a query that inserts rows into a table based on "missing" rows.

There is some code below, please note that when I have the (n), that is where I want the row number to fill in. So for example in line 7, I want it to subtract the dates of row 2 from row 1. If that date difference is greater than 4, do the subtraction and divide by 3 to figure out how many null lines I have to insert. Then once I figure out how many lines to insert, insert the nulls, and cycle through subtracting 3 months every time to get the correct adate. When that is done, check the difference between rows 2 and rows 3.

So this is temp_rdq:

adate   rdq
2020-06-30  2020-08-10
2020-03-31  2020-05-26
2019-09-30  2019-11-27
2019-06-30  2019-08-13
2018-12-31  2019-07-18

do you see how between row 2 and row 3 there is a 6 month gap between rdqs? I would like to write a query that recognizes there is a 6 month gap in there and inserts a "null" row like such: adate rdq 2020-02-27 NULL if there is a 9 month gap, insert 2 rows, where the adate follows the 3 month pattern

declare @tmpRowCount int
    set @tmpRowCount=(SELECT count(*) from #temp_RDQ);
    declare @n  int 
    set @n=1
While (@n<=@tmpRowCount)
BEGIN
if datediff(month,#temp_rdq.rdq(n+1),#temp_rdq.rdq(n))>4
set @inserts= FLOOR( datediff(month,#temp_rdq.rdq(n+1),#temp_rdq.rdq(n)/3));
    while (@inserts>0)
      BEGIN
    WITH K AS(
        SELECT 
            ROW_NUMBER() OVER (ORDER BY adate desc) row_num, adate,rdq
        FROM #temp_rdq)
    
           insert into #temp_RDQ (adate,rdq)
           SELECT DATEADD(month,-3*@inserts,#temp_rdq.rdq(n)), NULL 
           FROM K
           WHERE row_num =@n
           SET @inserts=@inserts-1;
      END
      SET @n=@n+1
END

Any help is greatly appreciated! I have tried using the LEAD function, but I still need to specify the specific row I am talking about in order to know how many null rows to insert.

One of the struggles with this is that at the very end, I need to be able to input a number, such that it will give me the earnings date 7 rows ago.Like so:

WITH t AS(
    SELECT 
        ROW_NUMBER() OVER (ORDER BY adate desc) row_num, adate,rdq
    FROM #temp_rdq)
SELECT 
  adate, rdq 
FROM 
    t
WHERE 
    row_num =@variable_to_be_input_later

Thanks!

Sample data and expected results will be a whole lot clearer than multiple dense paragraphs of text. - Dale K
notifying you of changes made. Hopefully this is better. Thanks! - Karina K
Look up "gaps and islands". There is a set-based solution for this, basically "Insert a row for each month where there isn't already a row for that month". And set-based solutions are ALWAYS preferable to iterative solutions in SQL. There are actually a variety of ways to approach this via sets (one single INSERT and SELECT that just "does the right thing"), and you should avoid WHILE loops for stuff like this. - pmbAustin