I’m trying to select the distinct products, grouped by all columns except the ID, and the cheapest bid for that type of product. Sometimes, there may be no bids for that product, so I’ll need to left join to account for those.
I’ll add the desired output below and also a sort-of pseudo query to help explain.
If it helps, you can imagine a product search page. When the user searches, it groups all the same products together. These products can be sold by many different sellers but we want to only show it once within the results, along with the cheapest price for that product at the time of the search.
Data I want returned
SELECT count(Name), Size, Weight, Color, BidID, Status, min(Price)
-- joins go here --
-- group by Size, Weight, Color, BidID, Status
Will also require WHERE clauses, which are built based on filters used by end user. Examples:
WHERE Status = 'Active' and Weight = '1kg'
WHERE Status = 'Active'
WHERE Size = '1m' and Weight in ('1kg', '2kg')
Products Table
| ProductID (PK) | Name | Size | Weight | Color |
| -------- | -------- | -----| ----- | ----- |
| 1 | Black Table | 1m | 2kg | Black |
| 2 | Purple Table| 1m | 3kg | Purple |
| 3 | Small Table | 1m | 3kg | Black |
| 4 | Small Table | 1m | 3kg | Black |
| 5 | Black Table | 1m | 2kg | Black |
| 6 | Purple Table| 1m | 3kg | Purple |
| 7 | Small Table | 1m | 3kg | Black |
| 8 | Small Table | 1m | 3kg | Black |
Bids Table
| BidID (PK) | ProductID | Status | Price |
| -------- | ------- | -------- | ----- |
| 1 | 1 | Active | 123.5 |
| 2 | 1 | Active | 325.99 |
| 3 | 1 | Active | 85.99 |
| 4 | 3 | Cancelled | 315.99 |
| 5 | 4 | Active | 113.5 |
| 6 | 3 | Cancelled | 305.99 |
| 7 | 1 | Active | 82.99 |
| 8 | 2 | Active | 345.99 |
DESIRED OUTPUT EXAMPLE
| Count | Name | Size | Weight | Color | BidID |CheapestPrice |
| -------- | -------- | -----| ----- | ----- | ----- | ----- |
| 38 | Black Table | 1m | 2kg | Black | 1 | 123.5
| 21 | Purple Table| 1m | 3kg | Purple | 2 | 89.95
| 13 | Small Table | 1m | 3kg | Black | 3 | 65.94
2
Answers
Assuming you have version 8 or above (and you should by now) allocate a row number to the lowest bid an union products with no bids
Whole Query :
The above code is for :
Select count of grouped Products AND the cheapest price for each product
Check it out.