Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

subquery

$
0
0
I am trying to solve the following problem:

Find the largest order amount for each salesperson and the associated order number, along with the customer to whom that order belongs to. Right now all my code does is:

Display salesperson_id more than once and display all order number and all sales amounts. What it should be doing is giving me only 1 salesperson_id with the highest sales amount and include the order number along with it.

Any help would be greatly appreciated.


select distinct orders.salesperson_id, Number as OrderNum, Amount
from
Orders
left JOIN
(
SELECT salesperson_id, max(number) As MaxNum, MAX(Amount) AS MaxOrder
FROM Orders
GROUP BY salesperson_id, Number, Amount
) as TopOrderAmountsPerSalesperson
On Orders.cust_id = TopOrderAmountsPerSalesperson.salesperson_id
group by orders.salesperson_id, Number, Amount

Viewing all articles
Browse latest Browse all 13329

Latest Images

Trending Articles



Latest Images