34,590
社区成员
发帖
与我相关
我的任务
分享
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
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 行受影响)
*/
select SUM([count]) as sum1
from
(
select distinct ID,[COUNT] from table1
) aa
sum1
-----------
5564
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 行受影响)
*/
select id,sum([count]) from tb group by id
select id,sum([count]) from tb group by id
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
--这个意思?
select id,sum([count]) from tb group by id