Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.server -> Re: SQL - Combine multiple records to give 1 record

Re: SQL - Combine multiple records to give 1 record

From: Arun Mathur <themathurs_at_gmail.com>
Date: 7 Sep 2005 08:26:41 -0700
Message-ID: <1126106801.420568.48720@g47g2000cwa.googlegroups.com>


"select distinct t1.person_id,

        min(t2.start_month) as start_date,
        max(t2.end_month) as end_date

from temp t1, temp t2
where t1.start_month <= t2.end_month

        and t2.start_month <= t1.end_month group by t1.person_id, t1.start_month, t1.end_month

RH"

This query will give the correct results with the given data. However, it doesn't take into account that more than two records can combine to form one. I basically used a hierarchical query to connect the records based on their start and stop times. From there, I could figure out the latest month that each record extends to, and then combine them by returning the earliest month with each maximum latest month.

I'm pasting a SQL*Plus script I wrote to illustrate what I did. It'll prompt for an html file to spool to. Also note that it drops and creates a table called person, so as with any script, please do view it before running. As always, I appreciate any comments/suggestions.

Regards,
Arun

set echo on
set markup html on
set pagesize 0
spool &html_filename_to_output_to
drop table person;
create table person(person_pk number(11) not null, person_id number(11) not null,
start_month date,
end_month date);
alter table person add constraint person_primary primary key(person_pk);
--
--

select * from person
order by start_month,end_month;

create or replace view v_schedule_path as select schedule.*,sys_connect_by_path(t2_person_pk,'/') time_path, decode(instr(sys_connect_by_path(t2_person_pk,'/'),'/',2),0, substr(sys_connect_by_path(t2_person_pk,'/'), instr(sys_connect_by_path(t2_person_pk,'/'),'/',1)+1), substr(sys_connect_by_path(t2_person_pk,'/'),
instr(sys_connect_by_path(t2_person_pk,'/'),'/',1)+1,
instr(sys_connect_by_path(t2_person_pk,'/'),'/',2)
- instr(sys_connect_by_path(t2_person_pk,'/'),'/',1) -1)) root_branch,
level depth from
(
select t1.person_pk t1_person_pk,t1.person_id
t1_person_id,t1.start_month t1_start_month,t1.end_month t1_end_month,
t2.person_pk t2_person_pk,
t2.person_id t2_person_id,

t2.start_month t2_start_month,
t2.end_month t2_end_month
from person t1,person t2
where t1.person_id = t2.person_id
-- and t2.start_month >= t1.start_month

and t2.start_month <= t1.end_month
and t2.end_month > t1.end_month
) schedule
connect by prior t1_person_pk = t2_person_pk and t1_person_id = t2_person_id;

select * from v_schedule_path
order by t1_person_id,t1_start_month,t2_end_month;

Received on Wed Sep 07 2005 - 10:26:41 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US