我需要选择营业额最高的表中的2个客户,我需要按其他字段(收入总和)订购。我怎么能这样做?
这就是我的表格:
CustomerId Turnover Revenue
c1 1000 10
c2 1000 200
c1 50 20
c3 500 20
c1 1000 10
我需要选择给我:
CustomerId Turnover Revenue
c2 1000 200
c1 2050 40
答案 0 :(得分:3)
你可以使用dinamic select
select * from (
SELECT CustomerId, sum(Turnover) turnover , sum(Revenue) revenue
FROM table
GROUP BY CustomerId
ORDER by turnover
LIMIT 2) t
order by revenue;