简体   繁体   English

SQL Server-遍历临时表并计算多个值以将行插入第二个表

[英]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. 我有一个临时表#TEMP1,它可以拉回患者的下一个治疗日期以及给定日期范围内计划的治疗总数。 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. 我需要做的是获取下一个治疗日期,确定在下一个治疗日期之后有多少计划的治疗,并使用计划日期填充第二个临时表#TEMP2。

For example, in #TEMP1, contains the following data: 例如,在#TEMP1中,包含以下数据:

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. 我想遍历#TEMP1并为每一行计算计划的治疗日期。 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_3)或九次(PATIENT_2),并且每个循环都会在#TEMP2中产生新的一行。

PATIENT_1 would have the following results in #TEMP2 after a successful loop: 成功循环后,PATIENT_1在#TEMP2中将具有以下结果:

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. 在#TEMP2中,PATIENT_2将具有9行,而PATIENT_3将仅具有1行。

Is it possible to do this in SQL Server 2008? 在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). 在使用行数之前,我已经完成了非常基本的循环,但是从技术上讲,这并不是在一个表中循环,所以我不确定如何开始或什至有可能(我没有很多编写高级代码的经验。 SQL代码)。

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. 实际上,我在系统中将cteTally作为视图使用,因此不必重新创建它。 You can adjust the cte portion as needed to ensure you have enough rows. 您可以根据需要调整cte部分,以确保有足够的行。

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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