GROUP BY 多列

有人可能想要 GROUP BY 多个列

declare @temp table(age int, name varchar(15))

insert into @temp
select 18, 'matt' union all
select 21, 'matt' union all
select 21, 'matt' union all
select 18, 'luke' union all
select 18, 'luke' union all
select 21, 'luke' union all
select 18, 'luke' union all
select 21, 'luke'

SELECT Age, Name, count(1) count
FROM @temp 
GROUP BY Age, Name

将按年龄和姓名分组,并将产生:

年龄 名称 计数
18 卢克 3
21 卢克 2
18 亚光 1
21 亚光 2