简体   繁体   中英

SQL Server - loop through temp table and calculate multiple values to insert rows into second table

I have a temp table, #TEMP1, which pulls back the next treatment date for a patients and the total number of planned treatments in a given date range. Within the temp table is a value which gives me the minimum number of days between each treatment.

What I need to do is take the next treatment date, determine how many planned treatments there are after the next treatment date and populate a second temp table, #TEMP2, with the planned dates.

For example, in #TEMP1, contains the following data:

patient    next_tx_dt  planned_tx_cnt   min_days_bt   start_range  end_range
PATIENT_1  2/15/2015   4                28            2/1/2015     6/1/2015
PATIENT_2  2/05/2015   9                12            2/1/2015     6/1/2015
PATIENT_3  5/17/2015   1                112           2/1/2015     6/1/2015           

I would like to loop through #TEMP1 and for each row, calculate the dates of planned treatment. So one row could be looped through once (PATIENT_3) or nine times (PATIENT_2) and each loop results in a new row in #TEMP2.

PATIENT_1 would have the following results in #TEMP2 after a successful loop:

pat_id      planned_tx_dt   planned_tx_cnt
PATIENT_1   2/15/2015       1
PATIENT_1   3/15/2015       2
PATIENT_1   4/12/2015       3
PATIENT_1   5/10/2015       4

PATIENT_2 would have 9 rows and PATIENT_3 would only have 1 row in #TEMP2.

Is it possible to do this in SQL Server 2008? I have done very basic loops before using a row count but this technically isn't looping through a table so I'm not sure how to get this started or if it is even possible (I do not have a whole lot of experience writing advanced SQL code).

Make sure you read the link I posted. Meanwhile here is a functional example using the information you posted. I actually have that cteTally as a view in my system so I don't have to ever recreate it. You can adjust the cte portion as needed to ensure you have enough rows.

if OBJECT_ID('tempdb..#temp1') is not null
    drop table #temp1

create table #temp1
(
    patient varchar(50)
    , next_tx_dt date
    , planned_tx_cnt int
    , min_days_bt int
    , start_range date
    , end_range date    
)

insert #temp1
select 'PATIENT_1', '2/15/2015', '4', '28', '2/1/2015', '6/1/2015' union all
select 'PATIENT_2', '2/05/2015', '9', '12', '2/1/2015', '6/1/2015' union all
select 'PATIENT_3', '5/17/2015', '1', '112', '2/1/2015', '6/1/2015';

WITH
    E1(N) AS (select 1 from (values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))dt(n)),
    E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
    E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
    cteTally(N) AS 
    (
        SELECT  ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
    )

select *, DATEADD(DAY, (N - 1) * min_days_bt, next_tx_dt)
from #temp1 t1
join cteTally t on t.N <= t1.planned_tx_cnt
--where t1.patient = 'PATIENT_1'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM