Computing.Net > Forums > Database > Finding top salespeople

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Finding top salespeople

Reply to Message Icon

Name: boaster
Date: October 28, 2008 at 10:19:09 Pacific
OS: n/a
CPU/Ram: n/a
Product: n/a
Comment:

I'm trying to write a SQL statement to show me a list of sales for the top 20 salespeople in my department. The salespeople are in one table, their sales in another. The following doesn't work:

SELECT s.first, s.last, sl.salesid, sl.amount
FROM salespeople s, sales sl
WHERE s.id = sl.salespersonid
ORDER BY sl.amount
LIMIT 0,20

This gives me the first 20 sales, not 20 salespeople and all their amount, however if I use a GROUP BY the sales aren't broken down individually.

Thanks



Sponsored Link
Ads by Google

Response Number 1
Name: chao
Date: November 12, 2008 at 08:58:28 Pacific
Reply:

If you want a list of all the sales by the top 20 salespeople, you may well need to use a subquery or a view.

Note: this syntax should work in mysql 5.0 and higher. I can't make any promises about earlier versions (in fact, i all but guarantee it won't work in 3.23), and i've never touched oracle.

SELECT s.first, s.last, sl.salesid, sl.amount
FROM salespeople s
INNER JOIN sales sl ON s.id = sl.salespersonid
INNER JOIN
(
SELECT salespersonid, SUM(amount) total
FROM sales
GROUP BY salespersonid
ORDER BY total DESC
LIMIT 0,20
) top_20 ON s.id = top_20.salespersonid


0
Reply to Message Icon

Related Posts

See More


IIF Statement with Date i... can I do this with mysql?



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Database Forum Home


Sponsored links

Ads by Google


Results for: Finding top salespeople

how to find the nth max value www.computing.net/answers/dbase/how-to-find-the-nth-max-value-/46.html

Access Query/Find Similar Records www.computing.net/answers/dbase/access-queryfind-similar-records/177.html

automatically fill related fields www.computing.net/answers/dbase/automatically-fill-related-fields/227.html