I have a table Saledetail with columns prodid ID of the Product and Quantity quantity sold for that Product . Now I am trying to fetch the ProductId with minimum sale made.
After going through some resource like: SQL query to select distinct row with minimum value
I wrote this query
select prodid, sum(quantity) as total_sum_for_prod
from saledetail
group by prodid
and its output is:
+--------+-----------------------+
| PRODID | total_sum_for_prod |
+--------+-----------------------+
| 102 | 11 |
| 101 | 5 |
| 104 | 4 |
| 106 | 4 |
| 103 | 2 |
+--------+-----------------------+
Now in order to get prodid with the minimum value of total_sum_for_prod column, I will have to use select min(total_sum_for_prod) from above output. And when I am trying to get that row from the subquery, I am getting an error.
I would like to know how to get my requirement done?
In case any better solution that is also welcome.
Thank you