Im currently trying to pull some data into a table but i only want to show a certain row if it contains the highest amount in a column.
+----+--------+---------+---------+----------+-----+
| id | auc_id | user_id | bamount | date | out |
+----+--------+---------+---------+----------+-----+
| 20 | 10002 | 10008 | 12 |1342445619| 1 |
| 21 | 10002 | 10009 | 14 |1342445667| 1 |
| 22 | 10002 | 10008 | 16 |1342445669| 0 |
+----+--------+---------+---------+----------+-----+
I would like to show in the table the most recent row for each user_id for each auc_id
so user for auc_id 10002 user 10008 would show the bamount of 16 only in the table and ignore the bamount of 12 row because 12 is smaller than 16
Is this possible?
Yes, it’s possible to show the row that corresponds to each user’s maximum bid.
Start by doing what @Bryan Moyles suggested; summarize your bids by user.
Now you know each user’s biggest bid. Next you need to retrieve the whole row, by joining your original table to the summary.
This is almost correct. But if you have a user who has bid precisely the same amount on two different auctions, you’ll get two rows for that user. Your question didn’t specify what you need to do with this particular edge case.
How about this? Let’s give the most recent (that is, the one with the largest numerical id) bid for the user in the case where two bid amounts are identical. So, we need another summary query, like so.
Now you join that query to your raw table, and, lo and behold, you’re done. Don’t forget to use an appropriate ORDER BY clause in this overall query.
This presumes that id is a primary key unique column.
See how that goes? There’s a summary finding the largest numeric bid for user. It’s nested into a summary that finds the largest id number for each user. It, in turn is used to select the rows you need from your raw table.
There may be easier ways to pull this off by taking advantage of a better understanding of your data than you’ve given in your question. For example, if you know bids always increase, and no two bids have the same date, and you don’t care about getting the right auction if a user is bidding on more than one, then you may be able to just summarize by the date field. But, in my experience, it’s better to avoid reliance on assumptions about your data (such as bids always increasing) because things get goofed up in real life.