日期之后的最近日期

时间:2016-04-25 18:43:50

标签: sql

我正在研究SQL查询以提取患者数据。我有3张桌子。一个包含患者数据的唯一记录(即:名字,姓氏,出生日期......)。第二个表是手术台,用于记录患者手术的日期和结果。第三张表是调查表,记录了手术后进行的调查。 Patient表中的键称为HUN,它将Surgery和Investigation表以1对多的关系链接。

我需要了解的是,每次手术后最近的调查日期是什么。一名患者可以进行多次手术,我需要知道每次手术后的最新调查日期。

这是我的数据(注意:这是虚构的数据)。有2个手术日期:2005年5月11日和2010年11月22日。在这些手术日期之后我需要知道最近的调查日期。结果应为2条记录:

242424 11-May-05 2011-07-19

和...

242424 22-Nov-10 2011-02-07

HUN     Surgery Date Investigation Date    
242424  11-May-05   2005-01-22     
242424  11-May-05   2006-03-29     
242424  11-May-05   2007-03-05     
242424  11-May-05   2008-04-01     
242424  11-May-05   2009-06-04     
242424  11-May-05   2009-06-19     
242424  11-May-05   2010-05-21     
242424  11-May-05   2011-02-07     
242424  11-May-05   2011-02-15     
242424  11-May-05   2011-07-19     
242424  11-May-05   2012-06-12     
242424  11-May-05   2012-09-18     
242424  11-May-05   2013-04-04     
242424  11-May-05   2013-10-30     
242424  11-May-05   2014-10-07     
242424  11-May-05   2015-09-09     
242424  22-Nov-10   2005-01-22     
242424  22-Nov-10   2006-03-29     
242424  22-Nov-10   2007-03-05     
242424  22-Nov-10   2008-04-01     
242424  22-Nov-10   2009-06-04     
242424  22-Nov-10   2009-06-19     
242424  22-Nov-10   2010-05-21     
242424  22-Nov-10   2011-02-07     
242424  22-Nov-10   2011-02-15     
242424  22-Nov-10   2011-07-19     
242424  22-Nov-10   2012-06-12     
242424  22-Nov-10   2012-09-18     
242424  22-Nov-10   2013-04-04     
242424  22-Nov-10   2013-10-30     
242424  22-Nov-10   2014-10-07     
242424  22-Nov-10   2015-09-09   

4 个答案:

答案 0 :(得分:0)

我相信汇总您的患者身份和手术专栏可以满足您的需求:

getApplicationContext()

答案 1 :(得分:0)

要获取最新数据的记录,您可以执行以下操作。

     $sql = 'select column_name from investigation_tbl order by tbl_field_name desc limit 2 ';
     //you can also get id specific details by passing a patient id by using WHERE in the query .

答案 2 :(得分:0)

根据提供的信息,这是脚本。

CREATE TABLE #Investigation
    (
      HUN INT
    , Surgery_Date DATE
    , Investigation_Date DATE
    );

INSERT  INTO #Investigation
        ( HUN, Surgery_Date, Investigation_Date )
VALUES  ( 242424, '11-May-05', '2005-01-22' )
,       ( 242424, '11-May-05', '2006-03-29' )     
,       ( 242424, '11-May-05', '2007-03-05' )     
,       ( 242424, '11-May-05', '2008-04-01' )     
,       ( 242424, '11-May-05', '2009-06-04' )     
,       ( 242424, '11-May-05', '2009-06-19' )     
,       ( 242424, '11-May-05', '2010-05-21' )     
,       ( 242424, '11-May-05', '2011-02-07' )     
,       ( 242424, '11-May-05', '2011-02-15' )     
,       ( 242424, '11-May-05', '2011-07-19' )     
,       ( 242424, '11-May-05', '2012-06-12' )     
,       ( 242424, '11-May-05', '2012-09-18' )     
,       ( 242424, '11-May-05', '2013-04-04' )     
,       ( 242424, '11-May-05', '2013-10-30' )     
,       ( 242424, '11-May-05', '2014-10-07' )     
,       ( 242424, '11-May-05', '2015-09-09' )     
,       ( 242424, '22-Nov-10', '2005-01-22' )     
,       ( 242424, '22-Nov-10', '2006-03-29' )     
,       ( 242424, '22-Nov-10', '2007-03-05' )     
,       ( 242424, '22-Nov-10', '2008-04-01' )     
,       ( 242424, '22-Nov-10', '2009-06-04' )     
,       ( 242424, '22-Nov-10', '2009-06-19' )     
,       ( 242424, '22-Nov-10', '2010-05-21' )     
,       ( 242424, '22-Nov-10', '2011-02-07' )     
,       ( 242424, '22-Nov-10', '2011-02-15' )     
,       ( 242424, '22-Nov-10', '2011-07-19' )     
,       ( 242424, '22-Nov-10', '2012-06-12' )     
,       ( 242424, '22-Nov-10', '2012-09-18' )     
,       ( 242424, '22-Nov-10', '2013-04-04' )     
,       ( 242424, '22-Nov-10', '2013-10-30' )     
,       ( 242424, '22-Nov-10', '2014-10-07' )     
,       ( 242424, '22-Nov-10', '2015-09-09' );

SELECT  *
FROM    #Investigation;

SELECT  HUN
      , Surgery_Date
      , MAX(Investigation_Date) AS LatestInvestigation
      , MIN(Investigation_Date) AS EarliestInvestigation
FROM    #Investigation
GROUP BY HUN
      , Surgery_Date;

答案 3 :(得分:0)

如果您的SQL Server支持公用表表达式(CTE),那么最好的方法是:

;with t as (
select HUN,Surgery_Date,Investigation_Date,
ROW_NUMBER() over(partition by hun,Surgery_Date order by  Investigation_Date ) rn
from #Investigation
where Investigation_Date>Surgery_Date
)
select * from t
where rn=1
order by Surgery_Date desc

注意:在MS SQL Server上创建并测试。