SQL查询根据同一表中其他列的值从表中提取日期?

标签 sql sql-server database

在我的情况下,我想从下表中提取开始日期和停止日期,如您所见,我只有 1 个日期列,其中包含取决于 ACTION 列的开始/更改/停止日期:

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

棘手的一点是,当 Action=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]

任何帮助将不胜感激

最佳答案

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) 

但是对于一个病人同一种药物有多次服药的情况也要进行测试

关于SQL查询根据同一表中其他列的值从表中提取日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17612222/

相关文章:

mysql - SQL ALTER TABLE 添加主键错误 1064

javascript - 在 chrome 浏览器中使用 indexedDB 有大小限制吗?

mysql - 在SQL中,有没有办法通过比较两个表之间的字段来进行UNION?

sql - 在 MS SQL Server 中使用 containstable 时搜索与号 (&)

mysql - 触发在特定日期删除数据库

sql-server - SSIS执行SQL任务错误 "Single Row result set is specified, but no rows were returned."

database - 从 Windows Azure 中访问 SQLServer

database - Postgres 中的 DELETE 查询无限期挂起

sql - 在vb.net中使用SQL查询从MS Access数据库中检索选定的记录

mysql - 条件sql查询