求一条sql语句!!

YZX3288 2011-09-23 04:59:28
如何累加去除重复后的数据 !!如:
id count
123 2258
123 2258
124 3306
124 3306

把重复id的值去除,再累加起来 !sql语句咋写 ??
...全文
199 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaopaopao 2011-09-26
  • 打赏
  • 举报
回复

create table #t(id int,count int)
go
insert into #t select 123, 2258
insert into #t select 123, 2258
insert into #t select 124, 3306
insert into #t select 124, 3306


select id,sum([count]) w from (select ROW_NUMBER() over (partition by id order by [count]) rw,id,[count] from #t) r
where r.rw=1 group by id


Jenercy 2011-09-25
  • 打赏
  • 举报
回复
select sum(count) from (select distinct * from tableName)
QianQianDe_lan 2011-09-25
  • 打赏
  • 举报
回复
select id ,sum(count) from Table
group by id
feihongluori 2011-09-25
  • 打赏
  • 举报
回复
use students;
go
if object_id('tb3')is not null
drop table tb3
create table tb3
(id varchar(10) ,count int)
insert into tb3
select 123,2258 union all
select 123,2258 union all
select 124,3306 union all
select 124,3306;
go
select id,sum(count)as sum from tb3
group by id
union all
select '累加',sum(count1)
from(select sum(count)as count1 from tb3 )b
/*id sum
---------- -----------
123 4516
124 6612
累加 11128

(3 行受影响)
*/


Rich_er 2011-09-25
  • 打赏
  • 举报
回复

select SUM([count]) as sum1
from
(
select distinct ID,[COUNT] from table1
) aa

sum1
-----------
5564



另外建表的时候,最好列名不要用sql中已有的名字,容易混淆。
areswang 2011-09-24
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 yzx3288 的回复:]
引用 5 楼 areswang 的回复:

SQL code


create table #t(id int,count int)
go
insert into #t select 123, 2258
insert into #t select 123, 2258
insert into #t select 124, 3306
insert into #t select 12……
[/Quote]
我写的不是语句?对了,2005及以后版本用WITH。
-晴天 2011-09-24
  • 打赏
  • 举报
回复
create table #t(id int,count int)
go
insert into #t select 123, 2258
insert into #t select 123, 2258
insert into #t select 124, 3306
insert into #t select 124, 3306
select sum(count) from (
select distinct id,count from #t
)t
go
/*
-----------
5564

(1 行受影响)

*/
YZX3288 2011-09-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 areswang 的回复:]

SQL code


create table #t(id int,count int)
go
insert into #t select 123, 2258
insert into #t select 123, 2258
insert into #t select 124, 3306
insert into #t select 124, 3306



with t as(
select R……
[/Quote]


该语句怎么写 ??
YZX3288 2011-09-24
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 acherat 的回复:]

select id ,sum([count]) cnt from T
group by id
[/Quote]

不是这个意思 !!是去除一个123、124,再累加的 !!相当于一个3306+2258的总和
YZX3288 2011-09-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 areswang 的回复:]

SQL code


create table #t(id int,count int)
go
insert into #t select 123, 2258
insert into #t select 123, 2258
insert into #t select 124, 3306
insert into #t select 124, 3306



with t as(
select R……
[/Quote]


是的 !!
Cideason 2011-09-23
  • 打赏
  • 举报
回复

select id ,sum(count) from Table
group by id
geniuswjt 2011-09-23
  • 打赏
  • 举报
回复
select id,sum([count]) from tb group by id
chuanzhang5687 2011-09-23
  • 打赏
  • 举报
回复
select id,sum([count]) from tb group by id
秋叶随风飘落 2011-09-23
  • 打赏
  • 举报
回复
使用函数 sum
select id,sum([count]) from T group by id
学学你们 2011-09-23
  • 打赏
  • 举报
回复
select id,sum([count]) from tablename group by id
--小F-- 2011-09-23
  • 打赏
  • 举报
回复
select id,sum([count]) from tablename group by id
AcHerat 元老 2011-09-23
  • 打赏
  • 举报
回复
select id ,sum([count]) cnt from T
group by id
areswang 2011-09-23
  • 打赏
  • 举报
回复


create table #t(id int,count int)
go
insert into #t select 123, 2258
insert into #t select 123, 2258
insert into #t select 124, 3306
insert into #t select 124, 3306



with t as(
select ROW_NUMBER() over(PARTITION by id,[count] order by (select 1)) rn,* from #t
)


select cast(ID as varchar(10)) id,[count] from t where rn=1
union all
select '累加值',SUM([count]) from t where rn=1

----
id count
123 2258
124 3306
累加值 5564
--这个意思?
快溜 2011-09-23
  • 打赏
  • 举报
回复
select id,sum([count]) from tb group by id
suiyanpeng 2011-09-23
  • 打赏
  • 举报
回复
delete from tb
where id in
(
select id from tb b
where exists(select * from tb where b.name=name and b.id>id)
)

在创建个临时表,然后进行累加输出
加载更多回复(2)

34,590

社区成员

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

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