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: ruskie <russell.hardy_at_gmail.com>
Date: 27 Aug 2005 15:08:01 -0700
Message-ID: <1125180481.101171.177300@g49g2000cwa.googlegroups.com>


The following query should provide the required output:

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

premgoel_at_gmail.com wrote:
> I have a table
>
> person_id start_month end_month
> 1 200301 200303
> 1 200302 200303
> 1 200303 200304
> 1 200306 200308
>
>
> For the first 3 records, the end_month of the previous record is less
> than the start_month of the next record. I want to write a SQL to
> combine them and give me the following result
>
> person_id start_month end_month
> 1 200301 200304
> 1 200306 200308
>
> I can easily do it in PL/SQL but wanted to do it in SQL. Please help.
>
> Thanks
> PK
Received on Sat Aug 27 2005 - 17:08:01 CDT

Original text of this message

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