2

In my situation, I want to extract Start Date and Stop Date from following table as you can see i have only 1 Date column which contain Start/Change/Stop Date which depends on ACTION column :

01 - Start (New drug start)
02 - Change (Change of dose)
03 - Stop ( Drug Stopped)

The tricky bit is that When Action=02 dose is changed so the Change date should become Start date for the current dose and stop date for the previouse dose.

I am really confused with it,,

CREATE TABLE TEST(ID VARCHAR(10), [TherapyAction] INT, [Drug] INT, [Dose] FLOAT, [TherapyDate] DATETIME)

INSERT INTO [dbo].[TEST] VALUES ('XXX' ,1, 1, 60, '01/09/2009 '),
                                ('57A' ,3, 1, 60, '09/07/2011'),
                                ('57A' ,1, 3, 5, '25/06/2010'),
                                ('57A' ,3, 3, 5, '09/07/2011' ),
                                ('57A' ,1, 4, 187.5, '19/02/2010'),
                                ('57A' ,2, 4, 250, '01/06/2010' ),
                                ('57A' ,3, 4, 250, '09/07/2011' ),
                                ('A5B' ,1, 1, 12.5, '26/01/2007' ),
                                ('A5B' ,2, 1, 25, '06/02/2007' ),
                                ('A5B' ,2, 1, 225, '20/08/2009'),
                                ('A5B' ,1, 4, 62.5, '04/07/2006'),
                                ('A5B' ,2, 4, 125, '12/07/2006'),
                                ('A5B' ,2, 4, 250, '01/05/2008'),
                                ('A5B' ,1, 7, 7.5, '11/09/2006'),
                                ('A5B' ,3, 7, 7.5, '26/01/2007'),
                                ('A5B' ,1, 7, 9, '09/04/2010'),
                                ('A5B', 3, 7, 9, '19/07/2010')

SELECT * FROM [dbo].[TEST]

Any help will be greatly appreciated

4

2 回答 2

1
set dateformat dmy 
declare @test TABLE (ID VARCHAR(10), [TherapyAction] INT, 
                     [Drug] INT, [Dose] FLOAT,    
                     [TherapyDate] DATETIME) 
INSERT INTO @test VALUES ('XXX' ,1, 1, 60, '01/09/2009 '),
                            ('57A' ,3, 1, 60, '09/07/2011'),
                            ('57A' ,1, 3, 5, '25/06/2010'),
                            ('57A' ,3, 3, 5, '09/07/2011' ),
                            ('57A' ,1, 4, 187.5, '19/02/2010'),
                            ('57A' ,2, 4, 250, '01/06/2010' ),
                            ('57A' ,3, 4, 250, '09/07/2011' ),
                            ('A5B' ,1, 1, 12.5, '26/01/2007' ),
                            ('A5B' ,2, 1, 25, '06/02/2007' ),
                            ('A5B' ,2, 1, 225, '20/08/2009'),
                            ('A5B' ,1, 4, 62.5, '04/07/2006'),
                            ('A5B' ,2, 4, 125, '12/07/2006'),
                            ('A5B' ,2, 4, 250, '01/05/2008'),
                            ('A5B' ,1, 7, 7.5, '11/09/2006'),
                            ('A5B' ,3, 7, 7.5, '26/01/2007'),
                            ('A5B' ,1, 7, 9, '09/04/2010'),
                            ('A5B', 3, 7, 9, '19/07/2010');

SELECT t.ID, t.Drug, t.Dose,t.TherapyDate as start_date,
       (select top 1 t2.TherapyDate             
        from @test t2           
        where t2.ID=t.ID and t2.Drug=t.Drug 
              and ((t2.Dose<>t.Dose and t2.TherapyAction=2) or t2.TherapyAction=3)
      and t2.TherapyDate > t.TherapyDate
    order by t2.TherapyDate) as stop_date
FROM @test t where t.[TherapyAction] in (1,2) 

但您也应该针对一名患者多次服用同一种药物的情况进行测试

于 2013-07-12T10:46:35.693 回答
0

我认为您会发现拥有 StartDate、EndDate 和 Action 的行会更容易。如果 EndDate 为 NULL,则该操作仍然是“活动的”。

您可以轻松编写查询以显示与给定日期相关的操作(或剂量)或提取给定主题的操作历史记录。

(这取决于您是否能够更改数据库的架构)

于 2013-07-12T10:01:36.930 回答