We have a table bids(id,auction_id, vendor_id,bid_price,created_at)
where one vendor will have multiple bids but we take the latest using the created_at
column
We need to find the position of the vendor from the bids of all vendors where his position is determined by the lowest bid from the same auction_id using MySQL .assumeing vendor id = 5 and auction id = 7
I have written the following query
SET @vendor_id := 5;
SET @auction_id := 7;
SELECT
vendor_id,
lowest_bid,
FIND_IN_SET(vendor_id, sorted_vendors) AS position
FROM (
SELECT
b1.vendor_id,
b1.bid_price AS lowest_bid,
(
SELECT GROUP_CONCAT(DISTINCT vendor_id ORDER BY bid_price)
FROM bids b2
WHERE b2.auction_id = @auction_id
) AS sorted_vendors
FROM bids b1
INNER JOIN (
SELECT vendor_id, MAX(created_at) AS latest_created_at
FROM bids
WHERE auction_id = @auction_id
GROUP BY vendor_id
) latest_bids ON b1.vendor_id = latest_bids.vendor_id AND b1.created_at = latest_bids.latest_created_at
WHERE b1.auction_id = @auction_id
) AS subquery
WHERE vendor_id = @vendor_id
ORDER BY lowest_bid
LIMIT 1;
But it is showing the wrong position of the vendor. Here is the DBFiddle
2
Answers
I could solve it using the following query
In this query, the innermost subquery latest_bids selects the latest bids from each vendor by assigning row numbers based on the descending order of created_at for each vendor. The middle subquery vendor_bids calculates the lowest bid (MIN(bid_price)) among the latest bids for each vendor and assigns positions using the ROW_NUMBER() function. Finally, the outer query filters the result to include only the position of vendor 5.
this should give you the dataset you want which you can then filter as you need. It has bid_postn = 1 for the highest bid per auction but you can reverse this to be the lowest by changing the partition clause to use asc not desc.