0

我对我的 MYSQL 数据库有一个 sql 查询,并且希望将结果限制为只有 10 个销售人员而不是 20 个。我尝试在查询中插入他们的姓名,但不断出错。也可以根据总数进行排名。我正在使用的 sql 查询是:

select sales_id, totalbuy, totalsell, totalbuy + totalsell as total from
(select sales_id, SUM(CASE WHEN side= 'buy' THEN 1 ELSE 0 END) AS totalbuy, 
SUM(CASE WHEN side= 'sell' THEN 1 ELSE 0 END) AS totalsell
from sales_id where sales_id='sales1', GROUP BY sales_id)q

上面写着 sales1 我尝试使用逗号和单引号添加剩余的 9 个 sales_ids,但无法正常工作。

4

1 回答 1

0

如果您只需要特定的 sales_id,请使用“IN”子句。

如果您只想要前 10 名,请删除该行并使用 LIMIT

select sales_id, totalbuy, totalsell, totalbuy + totalsell as total from
(select owner_id, SUM(CASE WHEN side= 'buy' THEN 1 ELSE 0 END) AS totalbuy, 
SUM(CASE WHEN side= 'sell' THEN 1 ELSE 0 END) AS totalsell
from sales_id  
where sales_id in ('sales1', 'sales2', 'sales3', 'sales4')-- if you wanna certain sales_id only
GROUP BY sales_id)q
order by total desc
limit 0, 10;--top 10 by total (bigger first)
于 2012-11-01T16:23:43.317 回答