1

假设我有一个名为 VISITS 的表,其中包含以下内容

  • VISIT_DATE(时间戳)
  • 高度(数字)
  • 腰围(数字)
  • 重量(数量)
  • PATIENT_ID (NUMBER)

    访问日期| 高度 | 腰围 | 重量 | PATIENT_ID

    2012 年 10 月 1 日 | (空) | 96 | 130 | 44123

    2012 年 11 月 1 日 | 1.74 | (空) | 120 | 44123

    2012 年 12 月 1 日 | (空) | (空) | 150 | 44123

我需要的是一个 sql select 语句来获取每列的最新值,但忽略空字段,所以我需要的输出是

WHERE 子句将是

WHERE PATIENT_ID = 44123 ORDER BY VISIT_DATE DESC LIMIT 1

12/01/2012 | 1.74 | 96 | 150 | 44123

正如您从输出中看到的那样,我希望从选择中获得我正在为患者向后退一步,直到找到每个字段的值。

我希望这个问题是明确的和可能的,我已经搜索但到目前为止还没有找到任何真正的解决方案,这将用于简化医院使用的医疗系统,上面大约有​​ 100 个领域只是一个几个字段的示例,以了解所需的概念。

4

3 回答 3

3
create table VISITS 
(
  VISIT_DATE TIMESTAMP
, HEIGHT NUMBER
, WAIST NUMBER
, WEIGHT NUMBER
, PATIENT_ID NUMBER
);


insert into visits values(systimestamp, null, 96, 130, 44123);
insert into visits values(systimestamp, 1.74, NULL, 120, 44123);
insert into visits values(systimestamp, null, NULL, 150, 44123);




select 
  last_visit_date
, height
, waist
, weight
, patient_id
from
(
  select 
    first_value( VISIT_DATE) over( partition by patient_id order by visit_date desc) last_visit_date
  , first_value( HEIGHT ignore nulls) over( partition by patient_id order by visit_date desc) height
  , first_value( WAIST ignore nulls) over( partition by patient_id order by visit_date desc) WAIST
  , first_value( WEIGHT ignore nulls) over( partition by patient_id order by visit_date desc) WEIGHT
  , min(visit_date) over( partition by patient_id)  min_visit_date
  , visit_date
  , patient_id
  from visits

)
where 
  min_visit_date = visit_date
and patient_id = 44123
;

这是SQLfiddle 示例

于 2012-08-14T08:20:44.123 回答
1

试试这个:

select a.PATIENT_ID,a.VISIT_DATE,b.HEIGHT,c.WEIGHT,d.WAIST
from
    (select    PATIENT_ID,
              max(VISIT_DATE) as VISIT_DATE 
    from      VISITS
    group by  PATIENT_ID)a
join
    (select PATIENT_ID,HEIGHT from (select PATIENT_ID,HEIGHT 
    from VISITS v
    order by  case when v.HEIGHT is null 
        then to_date('1900/01/01','YYYY/MM/DD') 
                    else v.VISIT_DATE end desc) where rownum=1)b
on a.PATIENT_ID=b.PATIENT_ID
join
    (select PATIENT_ID,WEIGHT from (select PATIENT_ID,WEIGHT 
    from VISITS v 
    order by  case when v.WEIGHT is null 
          then to_date('1900/01/01','YYYY/MM/DD') 
                       else v.VISIT_DATE end desc) where rownum=1)c
on a.PATIENT_ID=c.PATIENT_ID
join
    (select PATIENT_ID,WAIST from (select PATIENT_ID,WAIST 
    from VISITS v
    order by  case when v.WAIST is null 
          then to_date('1900/01/01','YYYY/MM/DD') 
                      else v.VISIT_DATE end desc) where rownum=1)d
on a.PATIENT_ID=d.PATIENT_ID          

SQL 小提琴演示

于 2012-08-14T06:00:30.593 回答
1
select visits2.patient_id,visits2.maxDate,
(  select max(HEIGHT) from visits where visit_Date=
    (select max(visit_date) m from visits l
    where (HEIGHT is not null)
           and
          (l.patient_id=visits2.patient_id)
     )
     and 
     (patient_id=visits2.patient_id)
) HEIGHT,

(  select max(WAIST) from visits where visit_Date=
    (select max(visit_date) m from visits l
    where (WAIST is not null)
           and
          (l.patient_id=visits2.patient_id)
     )
     and 
     (patient_id=visits2.patient_id)
) WAIST,
(  select max(WEIGHT) from visits where visit_Date=
    (select max(visit_date) m from visits l
    where (WEIGHT is not null)
           and
          (l.patient_id=visits2.patient_id)
     )
     and 
     (patient_id=visits2.patient_id)
) WEIGHT


from
(select patient_id,max(visit_date) maxDate 
 from visits group by patient_id) visits2

这是SQLfiddle 示例

于 2012-08-14T06:17:48.187 回答