7 messages in com.mysql.lists.mysqlNeed to find last price and date prod...
FromSent OnAttachments
mos27 Sep 2006 21:35 
Douglas Sims27 Sep 2006 21:53 
João Cândido de Souza Neto28 Sep 2006 05:39 
Jonathan Mangin28 Sep 2006 06:27 
Peter Brawley28 Sep 2006 08:12 
Douglas Sims28 Sep 2006 08:48 
mos28 Sep 2006 12:40 
Subject:Need to find last price and date product was sold
From:mos (mos@fastmail.fm)
Date:09/27/2006 09:35:56 PM
List:com.mysql.lists.mysql

This should be easy but I can't find a way of doing it in 1 step.

I have a Trans table like:

Product_Code: X(10) Date_Sold: Date Price_Sold: Float

Now there will be 1 row for each Product_Code, Date combination. So over the past year a product_code could have over 300 rows, one row for each day it was sold. There are thousands of products.

What I need to do is find the last price_sold for each product_code. Not all products are sold each day so a product might not have been sold for weeks.

The only solution I've found is to do:

drop table if exists CurrentPrices; create table CurrentPrices select Prod_Code, cast(max(Date_Sold) as Date), -1.0 Price_Sold from Trans group by Prod_Code; alter table CurrentPrices add index ix_ProdCode (Prod_Code); update CurrentPrices CP, Trans T set CP.Price_Sold=T.Price_Sold and T.Date_Sold=CP.Date_Sold;

Is there a way to shorten this? It may take 2-3 minutes to execute. I don't really need a new table as long as I get the Prod_Code and the last Date_Sold.

TIA Mike