Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Question about SQL Query
Assuming the base date is the first date for each user (else you have to change the query in with clause):
SQL> select user_id, purchase_date from t order by user_id, purchase_date;
USER_ID PURCHASE_D
---------- ----------
1 11/01/2005 1 11/03/2005 1 11/08/2005 1 11/09/2005 1 11/11/2005 1 12/01/2005 1 12/05/2005 1 12/18/2005
8 rows selected.
SQL> with min_date as ( select user_id, min(purchase_date) min_date
2 from t 3 group by user_id )4 select user_id, count(*)
6 from t, min_date 7 where min_date.user_id = t.user_id 8 group by t.user_id, trunc((t.purchase_date-min_date.min_date)/7) )9 group by user_id
1 4
1 row selected.
Regards
Michel Cadot
Received on Thu Dec 15 2005 - 00:34:03 CST