34,587
社区成员
发帖
与我相关
我的任务
分享
select id,sum(count) as count
from 表A
group by id
order by sum(count) desc
select id,sum(count) as count
from 表A
group by id
order by sum(count) desc
select id, sum([count]) as cnt from A group by id
select id,cnt=sum([count]) from A group by id
select id,sum([count]) [count] from A group by id
select id , sum(count) as count
from 表A
group by id
order by sum(count) desc
select id,sum([count]) [count] from A group by id
if object_id('tb') is not null drop table tb
go
create table tb([id] varchar(10),[count] int)
insert tb select 'a',1
union all select 'a',2
union all select 'c',1
union all select 'd',2
union all select 'b',1
union all select 'c',2
go
select id,sum([count]) [count] from tb group by id order by [count] desc
/*
id count
---------- -----------
a 3
c 3
d 2
b 1
(4 行受影响)
*/
调整一下.
select id , sum(count) as count from 表A group by id
select id,sum([count]) [count] from A group by id
create table test(id varchar(5),[count] int)
go
insert test select 'a' , 1
insert test select 'a' , 2
insert test select 'c' , 1
insert test select 'd' , 2
insert test select 'b' , 1
insert test select 'c' , 2
go
select id ,sum([count]) [count] from test group by id order by id
go
drop table test
/*
id count
----- -----------
a 3
b 1
c 3
d 2
(所影响的行数为 4 行)
*/
declare @a table (id varchar(10),[count] int)
insert into @a select 'a',1
union all select 'a',2
union all select 'c',1
union all select 'd',2
union all select 'b',1
union all select 'c',2
select id,sum([count]) as count from @a
group by id order by [count] desc
id count
---------- -----------
a 3
c 3
d 2
b 1
(4 行受影响)