tags:

views:

867

answers:

1

Hi I have following data in the table as below and I am looking for a way to group the continuous time intervals for each id to return:

CREATE TABLE DUMMY

( ID VARCHAR2(10 BYTE),

TIME_STAMP VARCHAR2(8 BYTE),

NAME VARCHAR2(255 BYTE)

);

ID, min(TIME_STAMP) "startDate", max(TIME_STAMP) "endDate", NAME group by ID , NAME

something like

100 20011128 20011203 David

100 20011204 20011207 Unknown

100 20011208 20011215 David

100 20011216 20011220 Sara

and so on ...

Thanks

ps. I have sample script, but i dont no how to attach my file. ds.

Hi every one here is more input:

  1. There is only one record with time_stamp for a specific ID.

  2. Users kan be diffrent, for example for day1 David, day2 unknown, day3 David and so on.

so there is one row for every day of year for each ID but with diffrent users.

Now, i want to se the break point, diffrences base on time_stamp intervals from day one

until last day for a specific ID in day order from begin day until last day.

Query Result should be :

ID NAME MIN_DATE MAX_DATE

100 David 20011128 20050407

100 Sara 20050408 20050417

100 David 20050418 20080416

100 Unknown 20080417 20080507

100 David 20080508 20080508

100 Unknown 20080509 20080607

100 David 20080608 20080608

100 Unknown 20080609 20080921

100 David 20080922 20080922

100 Unknown 20080923 20081231

100 David 20090101 20090405

thanks

Hi again, many tanks to every one, i have solved the problem, here is the solution:

select id, min(time_stamp), max(time_stamp), name

  from (

select id, time_stamp, name,

       max(rn) over (order by time_stamp) grp

  from (

select id, time_stamp, name,

       case when lag(name) over (order by time_stamp) <> name or

                 row_number() over (order by time_stamp) = 1

            then row_number() over (order by time_stamp)

       end rn

 from dummy

      )

      )

group by id, grp, name

order by 1
A: 
Select
   ID,
   Name,
   min(time_stamp) min_date,
   max(time_stamp) max_date
from
   Dummy
group by
   Id,
   Name

That should work.

IF you want the date range for each Id, but all the names you can do:

Select
   d.Id,
   d.Name,
   dr.min_date,
   dr.max_date
from
   Dummy d

   JOIN 
      (Select
         Id,
         min(time_stamp) min_date,
         max(time_stamp) max_date
      from
         Dummy
      group by
         Id 
      ) dr
      on ( dr.Id = d.Id)

Ron

Ron Savage
Hi RonThanks for your answer, but this quer will return a group of distinct rows.I could send you my script if you wish to clerify what i am looking for!?Your query will return somthing like :ID NAME MIN_DATE MAX_DATE100 David 20011128 20090405100 Sara 20050408 20050417100 Unknown 20080417 20081231as you see the intervals is wrong.
What interval are you looking for? the Min/Max date of just the ID? Just the Name? Or the entire data set?
Ron Savage
It appears like he's trying to get it broken into some kind of "week" grouping. The problem is he's not using a real pattern. 11-28 to 12-03 is 6 days, 12-04 to 12-07 is 4 days, 12-08 to 12-15 is 8 days, and 12-16 to 12-20 is 5 days. Either his example output is not very good or he's got some other criteria going on.
Marshall Fryman
Quey number 2 dosen't work, i added some more info in my question.Thanks and please for give me for my clumsy handle of this site, this is the first time i am using this site, i will get better, i promise.