
126 PUZZLE 30 AVERAGE SALES WAIT
PUZZLE
30 AVERAGE SALES WAIT
Raymond Petersen asked me the following question: Given a Sales table
with just the date of the sale and customer columns, is there any way to
calculate the average number of days between sales for each customer in
a single SQL statement? Use a simple table in which you can assume that
nobody makes a sale to the same person on the same day:
CREATE TABLE Sales
(customer_name CHAR(5) NOT NULL,
sale_date DATE NOT NULL,
PRIMARY KEY (customer_name, sale_date));
Let’s take a look at the date for the first week in June 1994:
Sales
customer_name sale_date
=========================
'Fred' '1994-06-01' ...