I have read several related threads on StackOverflow but none of them solves my problem. I have a Sales database as where I need to query for the customer who spent the most amount in buying stuff. For that, I need to find who bought which product using
SELECT sum(qty*rate)
AS exp from salesdetails as s JOIN sales as ss on (ss.invno = s.invno)
JOIN customer as c ON (ss.customerno = c.custno) GROUP BY(c.name)
ORDER BY sum(qty*rate);
It returns a table with the name of the person and what he spent in ascending order as
Output of command above:
While what I actually need is to only print a tuple when sum(qty*rate) is maximum. Currently I'm getting the results by sub querying like:
SELECT name, sum(qty*rate) FROM salesdetails as s JOIN sales as ss on (ss.invno=s.invno)
JOIN customer as c ON (ss.customerno = c.custno) GROUP BY(c.name)
HAVING sum(qty*rate) IN (SELECT max(exp) FROM (SELECT sum(qty*rate)
AS exp from salesdetails as s JOIN sales as ss on (ss.invno = s.invno)
JOIN customer as c ON (ss.customerno = c.custno) GROUP BY(c.name) ORDER BY sum(qty*rate)) aa);
Expected Output:
Is there any shorter way to get to the output?

