Jump to content

Looking for guidance on another stupid max() query


sptrsn

Recommended Posts

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?

 

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.