2013-07-12 48 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

相關問題