SQL Oracle:如何对患者转移数据进行排序

时间:2015-03-17 14:29:52

标签: sql oracle gaps-and-islands

我希望您能帮我写一下有关单位病人转移的查询。大多数人都知道,患者可以从一个单位转移到另一个单位,也可以从同一个单位从床转移到另一个床,但是对于这个查询,我想要只有单位转移并拥有第一个单位到达和每个不同单位的每次转移的最后更新并忽略任何床铺转移,这是一个例子:

PATIENT_ID  Start_Date   END_DATE   UNIT_NAME   BED_NUMBER
----------------------------------------------------------
1           01/01/2015   01/02/2015     A          1
1           01/02/2015   01/03/2015     A          2    
1           01/03/2015   01/03/2015     B          1
1           01/03/2015   01/04/2015     C          5
1           01/04/2015   01/06/2015     C          8
1           01/06/2015   01/07/2015     C          9
1           01/07/2015   01/08/2015     A          1

输出应为:

PATIENT_ID  Start_Date   END_DATE     UNIT_NAME
------------------------------------------------
1           01/01/2015   01/03/2015      A
1           01/03/2015   01/03/2015      B
1           01/03/2015   01/07/2015      C
1           01/07/2015   01/08/2015      A 

请注意,患者转移可在同一天内多次完成。

1 个答案:

答案 0 :(得分:3)

这是问题。您可以使用Tabibitosan方法。请参阅链接以获取完整 解释这种方法。

SQL Fiddle

Oracle 11g R2架构设置

create table hospi(
    patien_id   number,
    start_date  date,
    end_date    date,
    unit_name   varchar2(1),
    bed_number  number
);

insert into hospi values(1, date'2015-01-01', date'2015-01-02',  'A',  1);
insert into hospi values(1, date'2015-01-02', date'2015-01-03',  'A',  2);
insert into hospi values(1, date'2015-01-03', date'2015-01-03',  'B',  1);
insert into hospi values(1, date'2015-01-03', date'2015-01-04',  'C',  5);
insert into hospi values(1, date'2015-01-04', date'2015-01-06',  'C',  8);
insert into hospi values(1, date'2015-01-06', date'2015-01-07',  'C',  9);
insert into hospi values(1, date'2015-01-07', date'2015-01-08',  'A',  1);
insert into hospi values(2, date'2015-01-07', date'2015-01-08',  'A',  1);
insert into hospi values(2, date'2015-01-08', date'2015-01-08',  'B',  1);
insert into hospi values(2, date'2015-01-08', date'2015-01-09',  'B',  1);

<强>查询

select
    patien_id,
    min(start_date)     start_date,
    max(end_date)       end_date,
    unit_name
from (
    select 
        patien_id,
        start_date,
        end_date,
        unit_name,
        row_number() over (partition by patien_id order by start_date, end_date) -
        row_number() over (partition by patien_id, unit_name order by start_date, end_date)     grp
    from hospi
)
group by
    patien_id,
    unit_name,
    grp
order by
    patien_id,
    start_date,
    end_date

<强> Results

| PATIEN_ID |                START_DATE |                  END_DATE | UNIT_NAME |
|-----------|---------------------------|---------------------------|-----------|
|         1 | January, 01 2015 00:00:00 | January, 03 2015 00:00:00 |         A |
|         1 | January, 03 2015 00:00:00 | January, 03 2015 00:00:00 |         B |
|         1 | January, 03 2015 00:00:00 | January, 07 2015 00:00:00 |         C |
|         1 | January, 07 2015 00:00:00 | January, 08 2015 00:00:00 |         A |
|         2 | January, 07 2015 00:00:00 | January, 08 2015 00:00:00 |         A |
|         2 | January, 08 2015 00:00:00 | January, 09 2015 00:00:00 |         B |

注意:如果你没有将时间部​​分存储在start_date和end_date中,并且如果同一天的单位之间有很多转移,则无法确定订单 转移发生的地方。