选择每个MRN的第一行

时间:2014-06-15 10:09:42

标签: sql oracle select

SELECT   DISTINCT OH.MRN,OH.VISIT_NUMBER,OH.EVENT_TIME
FROM ONCOLOGY_HISTROY_MV OH
WHERE   OH.PROC='BC - Heamatology-Oncology Appt'
ORDER BY EVENT_TIME ASC

我试图在特定的PROC(即程序)中为每个MRN(患者)进行首次访问

请帮助:)

3 个答案:

答案 0 :(得分:1)

可能就是这么简单:

select mrn, min(visit_date) mindate
from oncology_history_mv
where proc = 'BC Heamatology-Oncology Appt'
group by mrn

如果您需要更多详细信息,请将上面的内容作为派生表并加入其中。

 select fields you need
 from oncology_history_mv onc join 
 (select mrn, min(visit_date) mindate
from oncology_history_mv
where proc = 'BC Heamatology-Oncology Appt'
group by mrn ) temp on onc.mrn = temp.mrn and vist_date = mindate
where proc = 'BC Heamatology-Oncology Appt'

这假设event_time不包括过程的日期,只包括一天中的时间。

答案 1 :(得分:1)

这是使用分析函数的好机会:

SELECT MRN, VISIT_NUMBER, EVENT_TIME
FROM (SELECT OH.MRN, OH.VISIT_NUMBER, OH.EVENT_TIME,
             ROW_NUMBER() OVER (PARTITION BY OH.MRN ORDER BY OH.EVENT_TIME) AS SEQNUM
      FROM ONCOLOGY_HISTROY_MV OH
      WHERE OH.PROC = 'BC - Heamatology-Oncology Appt'
     ) OH
WHERE SEQNUM = 1
ORDER BY EVENT_TIME ASC;

答案 2 :(得分:-2)

可以按照以下方式完成

SELECT  OH.MRN,,OH.EVENT_TIME
FROM ONCOLOGY_HISTROY_MV OH
WHERE   OH.PROC='BC - Heamatology-Oncology Appt'
ORDER BY EVENT_TIME AS
GROUP BY OH.MRN
HAVING min(OH.EVENT_TIME)

这将为您提供添加MRN记录的第一个日期,这意味着它将是第一次访问。