I need to write a query to calculate the average number of days between purchases for each customer (without using subqueries).
create table data { customer varchar(20) not null, bought date not null, primary key (customer,bought) }
For example,
insert into data (customer,bought) values ('John Smith', date '2011-02-01'), ('Alice Cooper', date '2011-02-01'), ('Bob Baker', date '2011-02-01'), ('John Smith', date '2011-02-02'), ('Bob Baker', date '2011-02-02'), ('Bob Baker', date '2011-02-03'), ('Bob Baker', date '2011-02-04'), ('Bob Baker', date '2011-02-05'), ('Bob Baker', date '2011-02-06'), ('Bob Baker', date '2011-02-07'), ('John Smith', date '2011-02-07'), ('Alice Cooper', date '2011-02-08');
must return so that John Smith waits 1 day, and then 5 days, so his average age is 3 days. Alice Cooper (!) Waited 7 days for her to be average - 7. Bob Baker is a daily runner, so his average is 1.
I did something like this
select distinct customer, avg (bought) as average from data;
but that will not work.
Any help would be greatly appreciated.