I have a database "inventory" with food items with the following columns:
index | date | category | subcategory | code | description | start | end
where the "date" is the date received into inventory. I want to select the record for each unique category and subcategory where "start" is null and the date is the oldest.
My initial attempt looked like this:
SELECT MIN(date) as date, category, subcategory, description, code, inventory.index
FROM inventory
WHERE start is null
GROUP BY category, subcategory
and it returned the record that I wanted for each unique category and subcategory, but with a few exceptions. If a record was added later which had an earlier date (so, it has a higher index than records with later dates), the query would correctly return the date from this record, but would return values from another record for the remaining columns, creating a hybrid row that doesn't exist in the table. It's like the "date" value is continuously checked and updated if necessary, but the other fields are not changed once values are loaded into the result. I'm hoping that someone can help me understand what's happening here.
I tried a few other approaches:
How to select data where a field has a min value in MySQL?
John Woo suggested using a nested SELECT clause inside the WHERE clause. So, mine looks like:
SELECT *
FROM inventory
WHERE
( SELECT MIN(date) FROM inventory )
AND
start is null
GROUP BY category, subcategory
There seems to be a problem with the AND operator, and this only evaluates the "start is null" condition, and ignores the other regardless of which order I put them in. When I remove one of the conditions, the other evaluates correctly.
SQL query to select distinct row with minimum value
Ken Clark suggested using an INNER JOIN, which I modified to:
SELECT inv.*
FROM inventory inv
INNER JOIN
(
SELECT MIN(date) AS date, category, subcategory, description, code, inventory.index
FROM inventory
WHERE start is null
GROUP BY category, subcategory
) inv2
ON inv2.index = inv.index
WHERE inv2.date = inv.date
This approach returns the same results as my initial attempt, except that the problematic rows are omitted (I guess because the dates are different when it comes time to evaluate the final WHERE clause).
I would appreciate any and all suggestions on where I can learn about what's happening in these cases. Thank you!