使用Compute by子句和行统计函数(count,sum,max,avg,min等),可以统计排序中结果完全相同的列,统计值作为查询结果以附加行的形式显示
语法:Compute avg|count|max|min|sum by 表达式
--模拟一张商品表
--id:编号
--name:商品名称
--type:类型
--amount:数量
create table temp
(
id int identity(1,1) primary key,
[name] varchar(50),
[type] int ,
amount int
)
go
--插入测试数据
insert into temp([name],[type],amount)
select 'aa',1,10 union all
select 'bb',1,15 union all
select 'cc',1,10 union all
select 'dd',2,10 union all
select 'ee',2,30 union all
select 'ff',2,10 union all
select 'gg',3,10 union all
select 'hh',3,10 union all
select 'jj',4,50 union all
select 'kk',4,10 union all
select 'll',4,40 union all
select 'mm',4,10
现在要查询数据ÿ