Actually, I think you have solved a different problem than Clyde is asking about. I read his post such that he wants the first and last bottle sold, regardless of type. Your query returns first and last BY type. Maybe that is what he wants, but I've always wrestled with the easiest way to return the entire row based on Min and/or Max value. This is the way I've always done it, but it seems cumbersome every time I do it.
Query 1:
SELECT min(time_of_sale) first_sold, max(time_of_sale) last_sold
Into tbl_first_last_sold_temp
FROM beverage_sales_table
Query 2:
SELECT a.*
From beverage_sales_table a, tbl_first_last_sold_temp b
where (a.time_of_sale = b.last sold)
or (a.time_of_sale = b.first_sold)
Problem with this is that unless the Max and Min(time_of_sale) are not unique, you will get multiple rows returned, but that is a function of the data.
The 2 query method above always seems to give better performance than the subquery method which would be:
SELECT a.*
From beverage_sales_table
where time_of_sale= (Select Max(time_of_sale )
from beverage_sales_table )
or time_of_sale = (Select Min(time_of_sale )
from beverage_sales_table )
This query would actually be fast becase it is not grouping on a large number of values like customer_number for example.
Is there a better way? I'm hoping so, because I have to do this sort of thing alot. A more typical problem for me would be to select the first and last purchase for each customer and capture all of the detail for those purchases:
Query 1:
SELECT customer_number,min(time_of_sale) first_sold, max(time_of_sale) last_sold
Into tbl_first_last_sold_temp
FROM beverage_sales_table
group by customer_number
Query 2:
SELECT a.*
From beverage_sales_table a, tbl_first_last_sold_temp b
where a.customer_number = b.customer_number
and ((a.time_of_sale = b.last sold)
or (a.time_of_sale = b.first_sold))
Greatly appreciate any input/help.
Thanks