基于shiftno计算量的SQL查询?

时间:2012-06-08 12:59:10

标签: sql sql-server

我的表格如下:

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的总结果?

1 个答案:

答案 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