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 |