我的表格如下:
shiftno sno Amount
100 7 20
8 50
101 9 10
10 30
11 20
我尝试过这个查询
select SUM(Amount)
from example
where shiftNo = (select (max(shiftNo)) from example
显示结果= 10,但实际结果是10 + 30 + 20 = 60.这属于shiftno = 101
如何获得60的总结果?
答案 0 :(得分:4)
如果您想要使用shiftNo
limit 1
{}。
如果您想要所有shiftNo
及其amount
,请离开limit
行:
select shiftNo, SUM(Amount) as MaxAmount
from exmple
group by shiftNo
order by shiftNo desc
limit 1