I have a table Saledetail with columns prodid, Quantity. I want to fetch the prodid with the minimum of sum of quantity from that table.
After going through some resource like: SQL query to select distinct row with minimum value
I wrote this query
select prodid, sum(quantity) as k
from saledetail
group by prodid
and its output is:
+--------+----+
| PRODID | K |
+--------+----+
| 102 | 11 |
| 101 | 5 |
| 104 | 4 |
| 106 | 4 |
| 103 | 2 |
+--------+----+
Now in order to get prodid with the minimum value of k, I will have to use select min(k) from table_name. But I do not have a table_name for that query, since it is a sub-query. I also tried aliasing them, then I got error and couldn't figure out.
I would like to know how to get my requirement done?
In case any better solution that is also welcome.
Thank you