If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Query help. newest distinct rows.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-04-10, 12:25
nzo nzo is offline
Registered User
 
Join Date: Jan 2010
Posts: 16
Query help. newest distinct rows.

A prices table will list the prices of products, and will be updated often.

Prices
id product_id qty price timestamp

Each product ID will have multiple entry's.

What I want to select is the newest entry's for each unique combination of 'product_id' and 'qty'.

How do I fit max(timestamp) in with this query:
(SELECT DISTINCT product_id, qty FROM prices)?

Thanks
Reply With Quote
  #2 (permalink)  
Old 01-04-10, 12:48
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
SELECT product_id
     , qty 
     , MAX(timestamp) AS latest
  FROM prices
GROUP
    BY product_id
     , qty
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-04-10, 13:15
nzo nzo is offline
Registered User
 
Join Date: Jan 2010
Posts: 16
awesome, thanks!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On