select sum(score) from tb10 where score in (select top 3 score from tb10 order by score desc)
declare @tb table(id int, col int) insert @tb select 1, 100 union all select 2, 100 union all select 3, 100 union all select 4, 100 union all select 5, 100 set rowcount 3declare @total int set @total=0 select @total=@total+col from @tb select @total as totalset rowcount 0 /* total ----------- 300 */
create table #TB(id int, col int) insert #TB select 1, 100 union all select 2, 100 union all select 3, 100 union all select 4, 100 union all select 5, 100 select id,sum(col) from ( select top 3 * from #tb ) T group by id
修改下select sum(col) '总和' from ( select top 3 * from #tb ) T
select top 3 * from table?这个跟sum函数有啥关系?
如果还不符合要求,请给出测试数据,谢谢!
返回在某一集合上对数值表达式求得的和。语法
Sum(«Set»[, «Numeric Expression»])示例
如果 USA、CANADA 和 MEXICO 成员的值分别为 1000、2000 和 3000,则以下示例返回 6000:Sum({USA, CANADA, MEXICO}, Sales.VALUE)以下示例更加直观,并且同样有效:Sum({USA, CANADA, MEXICO}, Sales)
insert @tb
select 1, 100 union all
select 2, 100 union all
select 3, 100 union all
select 4, 100 union all
select 5, 100 set rowcount 3declare @total int
set @total=0
select @total=@total+col from @tb
select @total as totalset rowcount 0
/*
total
-----------
300
*/
create table #TB(id int, col int)
insert #TB
select 1, 100 union all
select 2, 100 union all
select 3, 100 union all
select 4, 100 union all
select 5, 100 select id,sum(col) from
(
select top 3 * from #tb
) T
group by id
(
select top 3 * from #tb
) T