22,210
社区成员
发帖
与我相关
我的任务
分享
----------------------------------------------------------------
-- Author :fredrickhu(小F 向高手学习)
-- Date :2009-08-27 19:30:36
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([name] int,[item] int,[count] int)
insert [tb]
select 5566,6677,100 union all
select 5566,6677,200 union all
select 5566,2233,44 union all
select 4455,6677,50
--------------开始查询--------------------------
select
name,[item],
sum([count]) as [count]
from
tb
where
item='6677'
group by
[name] ,[item]
order by
[name] desc
----------------结果----------------------------
/*name item count
----------- ----------- -----------
5566 6677 300
4455 6677 50
(2 行受影响)
*/
if object_id('[TB]') is not null
drop table [TB]
go
create table [TB]([name] int,[item] int,[count] int)
insert [TB]
select 5566,6677,100 union all
select 5566,6677,200 union all
select 5566,2233,44 union all
select 4455,6677,50
select name,[item],[count]=sum([count])
from tb
where item='6677'
group by name ,[item]
order by name desc
/*
name item count
----------- ----------- -----------
5566 6677 300
4455 6677 50
*/
if object_id('[TB]') is not null
drop table [TB]
go
create table [TB]([name] int,[item] int,[count] int)
insert [TB]
select 5566,6677,100 union all
select 5566,6677,200 union all
select 5566,2233,44 union all
select 4455,6677,50
select name,[item]=max([item]),[count]=sum([count])
from tb
group by name
(4 行受影响)
name item count
----------- ----------- -----------
4455 6677 50
5566 6677 344
--> 测试数据:[TB]
if object_id('[TB]') is not null drop table [TB]
create table [TB]([name] int,[item] int,[count] int)
insert [TB]
select 5566,6677,100 union all
select 5566,6677,200 union all
select 5566,2233,44 union all
select 4455,6677,50
select name,[item]=max([item]),[count]=sum([count])
from tb group by name
order by sum([count]) desc
/*
name item count
----------- ----------- -----------
5566 6677 344
4455 6677 50
(2 行受影响)
*/
drop table TB