Question : Oracle 9 sql

Hi - hope someone can help I've been struggling with writing this query for a while..

I'm attempting to write a query which ;

For customers who have booked more than once   shows me ;
the date of their first booking, the dates of their 2nd and 3rd ( if it exists ) subsequent bookings made grouped by their date of first booking.

So results would look something like ;


1stBookDate   No_of_Cust_1  2ndBookDate  No_of_Cust2  3rdBookDate No_of_Cust3
----------------   ------------------   ----------------  ----------------   ----------------  ----------------
01/01/2001      10                    01/02/2001      7                     01/03/2001   2
01/01/2001       5                     02/02/2001      3                     09/02/2001   1

The record on the first line showing that of the 10 customers who booked on 01/01/2001 ( who were to go on to book again at least once ), 7 of these booked again on the 01/02/2001 and a further 2 booked for a third time on the 01/03/2001.


The source table shows, amongst other things ;


Customer_ID    Date_of_Booking     Booking_ID
-----------------    ---------------------     ----------------
231546              01/01/2001            45465
231546              01/02/2001            46895
231546              01/03/2001            47891


So far I've been focusing on using subqueries where the first booking date is identified by Min(Date_of_booking) and the second identified by
Min(booking_date) where greater than the booking date identified by first query.

Sure there must be a better way to do this ? Any help would be welcome..

Thank you,
d.

Answer : Oracle 9 sql

Hope this helps:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
WITH temp AS
  ( SELECT Customer_ID,Date_of_Booking,Booking_ID, No_of_Booking
    FROM (
  	SELECT Customer_ID,Date_of_Booking,Booking_ID, No_of_Booking, row_number() OVER ( partition BY Customer_ID ORDER BY Date_of_Booking) rnum
    FROM urtable) temp1
    WHERE rnum <= 3 )
SELECT t1.Date_of_Booking, t1.No_of_Booking, t2.Date_of_Booking, t2.No_of_Booking, t3.Date_of_Booking, t3.No_of_Booking
FROM temp t1 
LEFT OUTER JOIN temp t2 ON t1.Customer_ID = t2.Customer_ID
LEFT OUTER JOIN temp t3 ON t1.Customer_ID = t3.Customer_ID
Open in New Window Select All
Random Solutions  
 
programming4us programming4us