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 > Mysql SELECT

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-31-04, 04:36
sikander sikander is offline
Registered User
 
Join Date: Dec 2004
Posts: 2
Mysql SELECT

Hello all,

I have a little problem that i seem to be at a loss at solving efficiently. Here's the situation

table : order_status_tracking
cols : id (int), orderId (int), newStatus(int), date(date), time(time)

each time an order gets a new status there is a row inserted in this table. now i have to generate a table that would get the last inserted status between a given date for an order. i am getting stuck at getting the correct status from the data.. i get the correct id, orderId, date but not the status.. it shows me the first status in the rows selected based on the date criteria.


SELECT MAX(ost.id) as ostId, MAX(ost.date), ost.newStatus, o.serviceType FROM order_status_tracking ost INNSER JOIN orders o ON (o.id = ost.orderId) WHERE date >= $sDate AND date < $eDate GROUP BY ost.orderId



i can't do a MAX on o.newStatus as the max id is not always the last status selected. what happens is that if there are multiple rows for an orderId, it selects the *last* id, *last date*, but the *first newStatus* from that last

any ideas how to get around this ? thanks!
Reply With Quote
  #2 (permalink)  
Old 12-31-04, 04:45
sikander sikander is offline
Registered User
 
Join Date: Dec 2004
Posts: 2
nevermind



SELECT ost.id, ost.orderId, ost.newStatus
FROM order_status_tracking AS ost
INNER JOIN order_status_tracking AS ost2 ON ost.orderId = ost2.orderId
WHERE ost.date >= '2004-12-29' AND ost.date <= '2004-12-29'
GROUP BY ost.id, ost.orderId, ost.newStatus
HAVING ost.id = MAX( ost2.id )
ORDER BY `ost`.`id`
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