sql sum()函数问题

向上低调的飞 2008-09-28 01:58:55
我有表A,有字段a
我有累加a的结果,但是只要a的最高值的前三位
例如:
a
------
2
3
1
5
6
4

结果应该为:
a
-----
15

选择了 6,5,4 这三个数字

我使用的语句如下:
select sum ( select top 3 a from A order by a )
from A

但是,总报错,请问语句该如何去做呢?


...全文
146 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
向上低调的飞 2008-09-28
  • 打赏
  • 举报
回复
一楼的不错
linux_ch 2008-09-28
  • 打赏
  • 举报
回复

select sum(a) from (select top 3 a from A order by a desc)ta

wer123q 2008-09-28
  • 打赏
  • 举报
回复
[code = SQL]select sum(a) from (select top 3 a from A order by a desc)z[/code]
飘_飘 2008-09-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wgzaaa 的回复:]
select sum(a) from (select top 3 a from A order by a desc) A
[/Quote]
A为表的别名 自己随便取
wgzaaa 2008-09-28
  • 打赏
  • 举报
回复
是为表命的一个别名,而且是必须的
xin_shui 2008-09-28
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 heping173 的回复:]
向楼上的请教:ta是什么意思
可为任意的字符,
去掉则报错:
服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: ')' 附近有语法错误。


是为表命的一个别名,而且是必须的?
[/Quote]

是的。感觉1楼的方法更好些
heping173 2008-09-28
  • 打赏
  • 举报
回复
向楼上的请教:ta是什么意思
可为任意的字符,
去掉则报错:
服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: ')' 附近有语法错误。


是为表命的一个别名,而且是必须的?
pengxuan 2008-09-28
  • 打赏
  • 举报
回复

if object_id('tb') is not null
drop table tb
go
create table tb(a int)
go
insert into tb
select 2 union all
select 3 union all
select 1 union all
select 5 union all
select 6 union all
select 4
go

select sum(a) from (
select top 3 * from tb order by a desc
)a
正牌风哥 2008-09-28
  • 打赏
  • 举报
回复
只有一列時2000可用1樓方法

05:

select sum(a) from (select a,row=row_number()over(order by a desc) row from A) t where row<=3
heping173 2008-09-28
  • 打赏
  • 举报
回复
select sum(a) from A where a in (select top 3 a from A order by a desc)
wgzaaa 2008-09-28
  • 打赏
  • 举报
回复
select sum(a) from (select top 3 a from A order by a desc)ta

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧