sptrsn Posted April 5, 2011 Share Posted April 5, 2011 Just when I thought I had this figured out, some combination of data comes up and whad'ya know? it doesn't work again. Here is the data id buyer apn bid bidstatus 1 61 10101120 77000 ACTIVE 2 62 10101120 99000 CANCELLED 3 63 10101120 75000 ACTIVE 4 62 10115374 99000 ACTIVE 5 62 10206459 75000 ACTIVE I want the highest "active" bid for each apn. With this data, that should be row 1, 4 and 5 I have tried everything I know and been searching for hours. Here's what I have that doesn't work... select id, buyer_id, apn, bid, bidstatus from prop p inner join (select max(bid)b from prop group by apn)m on p.bid=m.b where bidstatus='active' Here is the result of that query... id buyer_id apn bid bidstatus 3 63 10101120 75000 ACTIVE 4 62 10115374 99000 ACTIVE 4 62 10115374 99000 ACTIVE 5 62 10206459 75000 ACTIVE Wrong in multiple ways. What the heck? Can anyone help me with this? Quote Link to comment https://forums.phpfreaks.com/topic/232727-looking-for-guidance-on-another-stupid-max-query/ Share on other sites More sharing options...
gizmola Posted April 5, 2011 Share Posted April 5, 2011 Your problem is that you are not having your inner group by include both the apn and the bid. You also want to eliminate cancelled bids in the group by query. select p.* from prop p inner join (select apn, max(bid) as bid from prop WHERE bidstatus = 'ACTIVE' GROUP BY apn) m on p.bid = m.bid AND p.apn = m.apn; Quote Link to comment https://forums.phpfreaks.com/topic/232727-looking-for-guidance-on-another-stupid-max-query/#findComment-1197077 Share on other sites More sharing options...
reyborn Posted April 5, 2011 Share Posted April 5, 2011 Yes group the result. Quote Link to comment https://forums.phpfreaks.com/topic/232727-looking-for-guidance-on-another-stupid-max-query/#findComment-1197156 Share on other sites More sharing options...
gizmola Posted April 5, 2011 Share Posted April 5, 2011 Yes group the result. Was this supposed to add something to what I already provide (the actual query needed?) Quote Link to comment https://forums.phpfreaks.com/topic/232727-looking-for-guidance-on-another-stupid-max-query/#findComment-1197375 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.