Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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,20This 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

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

![]() |
IIF Statement with Date i...
|
can I do this with mysql?
|

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