If I have a table with columns pid, sid as primary key, and price and color.
How do I create a SQL query that can fetch the cheapest price of each pid (product ID) which has color equal to green?
For example I have rows :
(1, 1, 10, "green")
(1, 2, 8, "green")
(1, 3, 11, "green")
(2, 1, 7, "green")
(2, 2, 15, "green")
(2, 3, 22, "green")
(3, 1, 10, "red")
(1, 2, 8, "red")
(1, 3, 11, "red")
Then I will receive : (1, 2, 8)->pid=1 and (2, 1, 7)->pid=2. No pid=3 because its color is red.
Thanks in advance.
Or if you want the whole row (and your sql platform supports ranking)…