Thread: Fetch the latest log for each product

Fetch the latest log for each product

From
Bernie Huang
Date:
Hi,

I bet people have asked this question several times, but oh well, please
do anwser again.  Thanks.  =)

I have a product table and a log file.

product_tb
-----------
prod_id
prod_name
...

log_tb
---------
log_id
prod_id
cust_id
transact_date
...

How do I fetch the latest log for each product according to transaction
date?


- Bernie

Attachment

Re: Fetch the latest log for each product

From
Jeff Hoffmann
Date:
Bernie Huang wrote:
> 
> Hi,
> 
> I bet people have asked this question several times, but oh well, please
> do anwser again.  Thanks.  =)
> 
> I have a product table and a log file.
> 
> product_tb
> -----------
> prod_id
> prod_name
> ...
> 
> log_tb
> ---------
> log_id
> prod_id
> cust_id
> transact_date
> ...
> 
> How do I fetch the latest log for each product according to transaction
> date?
> 
> - Bernie


here's how i would do it, i guess.
 select p.prod_name, l.prod_id, max(l.transact_date) as
last_transaction    from log_tb l, prod_tb p   where l.prod_id=p.prod_id 
group by p.prod_name, l.prod_id;

i think that's what you want, at least...

-- 

Jeff Hoffmann
PropertyKey.com