无法使用update语句获取所需的结果

时间:2014-06-18 10:24:32

标签: sql sql-server

我无法使用记录下方的记录来更新数据。

所需输出:患者将能够在医院多次入院/再入院,如果患者在首次就诊后多次入院,首次就诊记录将获得重新入院= 0且指数= 1。这次访问应该对该患者进行指数评估。使用此index_admission应计算30天的重新接纳。

当前输出: enter image description here

计算: 从index_admission出院日期到下一个admit_visit日期,

  

1)如果差异小于30天,则重新接纳= 1且索引= 0

 else readmission=0 and Index=1 should be update.

每次检查时都应使用最新的index_admission discharge_date进行检查。

为了得到这个结果,我写下了逻辑,但它在使用第一个索引录取后30天后更新了readmission = 0和Index = 1。

UPDATE Readmission
SET Index_AMI = (CASE
        WHEN DATEDIFF(DD, (SELECT
            Sub.Max_Index_Dis
        FROM (SELECT
            Patient_ID,
            MAX(Discharge_Date_Time) Max_Index_Dis
        FROM Readmission
        WHERE Index_AMI = 1 AND FPR.Patient_ID = Patient_ID
        GROUP BY Patient_ID) Sub), FPR.Admit_Date_Time) <= 31 THEN 0 ELSE 1
    END),
    Is_AMI_Readmission = (CASE
        WHEN DATEDIFF(DD, (SELECT
            Sub.Max_Index_Dis
        FROM (SELECT
            Patient_ID,
            MAX(Discharge_Date_Time) Max_Index_Dis
        FROM Readmission
        WHERE Index_AMI = 1 AND FPR.Patient_ID = Patient_ID
        GROUP BY Patient_ID) Sub)
        , FPR.Admit_Date_Time) <= 31 THEN 1 ELSE 0
    END)
FROM Readmission FPR
WHERE fpr.index_ami IS NULL

预期结果 enter image description here

请帮我解决这个问题。

最诚挚的问候, Nagendra

1 个答案:

答案 0 :(得分:0)

谢谢大家的帮助。 我有些人设法使用临时表来解决它。 如果有任何问题,尤其是表现,请采用这种方法,请随时发表评论/批评。

/*------------ To moving all Patient_Readmission data into temp table -------------*/
IF EXISTS (select  * FROM tempdb.dbo.sysobjects o
    WHERE o.xtype IN ('U') 
 AND o.id = OBJECT_ID(N'tempdb..#Readmission')
)
Begin
DROP TABLE #Readmission
End

IF NOT EXISTS (SELECT  * FROM tempdb.dbo.sysobjects o
    WHERE o.xtype IN ('U') 
 AND o.id = OBJECT_ID(N'tempdb..#Readmission')
)
BEGIN
select Patient_ID,ID,Encounter_Code 
,Admit_Date_Time,Discharge_Date_Time
,Is_AMI_Readmission,Index_AMI
into #Readmission
from Patient_Readmission
END

GO
/*----------- To get Max Index discharge_date for all patients where index=1----------*/
IF EXISTS (select  * FROM tempdb.dbo.sysobjects o
    WHERE o.xtype IN ('U') 
 AND o.id = OBJECT_ID(N'tempdb..#Each_Pt_Max_Dsrg')
)
Begin
DROP TABLE #Each_Pt_Max_Dsrg
End

IF NOT EXISTS (SELECT  * FROM tempdb.dbo.sysobjects o
    WHERE o.xtype IN ('U') 
 AND o.id = OBJECT_ID(N'tempdb..#Each_Pt_Max_Dsrg')
)
BEGIN
select Patient_ID,Index_AMI,Max(Discharge_Date_Time) Max_Index_Dis 
into #Each_Pt_Max_Dsrg
from #Readmission where Index_AMI=1 group by Patient_ID,Index_AMI
END


Declare @var int 
Set @var=(select Min(Patient_ID) from #Each_Pt_Max_Dsrg)
While @var is not null
    Begin
        Declare @Var_En int
        declare @maxDisch datetime
        Set @Var_En=(Select Min(ID) From #Readmission Where Index_AMI Is null and Patient_ID=@var)
        While @Var_En is Not NULL
        Begin
            set @maxDisch = dateadd(dd,-1,(select max(Max_Index_Dis) from #Each_Pt_Max_Dsrg where Index_AMI=1 and Patient_ID=@var))
            while (@maxDisch < (select max(Max_Index_Dis) from #Each_Pt_Max_Dsrg where Index_AMI=1 and Patient_ID=@var))
                begin
                    Update #Readmission
                        set Index_AMI=(Case When Datediff(DD,(Select Max(Max_Index_Dis) From #Each_Pt_Max_Dsrg Where Index_AMI=1 And Patient_ID=@var),FPR.Admit_Date_Time) between 0 and 31 Then 0 Else 1 End)
                        ,Is_AMI_Readmission=(Case When Datediff(DD,(Select Max(Max_Index_Dis) From #Each_Pt_Max_Dsrg Where Index_AMI=1 And Patient_ID=@var),FPR.Admit_Date_Time) between 0 and 31 Then 1 Else 0 End)
                    From #Readmission FPR where Patient_ID=@var and ID=@Var_En and Index_AMI is NULL

                    set @maxDisch = (select max(Max_Index_Dis) from #Each_Pt_Max_Dsrg where Index_AMI=1 and Patient_ID=@var)

                    delete from #Each_Pt_Max_Dsrg

                    insert into #Each_Pt_Max_Dsrg
                    select Patient_ID,Index_AMI,Max(Discharge_Date_Time) Max_Index_Dis 
                    from #Readmission where Index_AMI=1 group by Patient_ID,Index_AMI
                end

        SET @Var_En = (SELECT Min(ID) FROM #Readmission WHERE ID> @Var_En)
        End

    SET @Var = (SELECT MIN(Patient_ID) FROM #Each_Pt_Max_Dsrg WHERE Patient_ID > @var)
    End