2013-07-12 49 views
2

在我的情况,我想从下面的表中抽取开始日期和截止日期,你可以看到我只有1个日期列包含依赖于操作列开始/变更/停止日期:SQL查询根据表中其他列的值提取表中的日期?

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

的棘手的是,当动作= 02剂量改变时,改变日期应该成为当前剂量的开始日期和前一剂量的停止日期。

我真的与它混淆,,

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] 

任何帮助,将不胜感激

回答

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) 

但你也应该测试它的情况时有服用药物以相同的药物对一个病人的几个时期

+0

嗨管理员非常感谢您的答案.. – arm

0

我想你会发现很容易有一个起始日期,结束日期和行动行。如果EndDate为NULL,则该操作仍然是“实时”。

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

(这取决于你能够改变你的数据库的架构)

+0

此数据集包含多个患者或单个患者的数据吗?第一列(57A,ASB等)是否代表病人,以便查询必须确定每个病人的开始/停止? – Tim

+0

是57A,A5B代表两个人...我可以提取开始日期和结束日期,但是发布了Actiontype 2 ,,,所以我们可以说Changedate是cussrent剂量的开始/结束日期。 – arm

+0

你好蒂姆,我试过这个,但没有得到什么是必需的....任何帮助将是非常有益的.......................... ................ SELECT ID, \t \t min(当[TherapyAction] = 1或[TherapyAction] = 2时,[TherapyDate] else null结束)作为StartD, \t \t MAX(情况下,当[TherapyAction] = 3或[TherapyAction] = 2,那么[TherapyDate]否则返回null端)作为EndD, \t \t [药物], \t \t [剂量] FROM [DBO]。[测试] group by ID,[药物],剂量 – arm

相关问题