SQL Server中GROUP BY(链接)
下面的这篇微软官方文档,介绍了SQL Server中的GROUP BY语句:
SELECT - GROUP BY- Transact-SQL
其中这里有说到,如果GROUP BY后面的列中有NULL值,那么SQL Server会将GROUP BY列中所有的NULL值认为是相等的,并将NULL归类到一个GROUP中:
If a grouping column contains NULL values, all NULL values are considered equal, and they are collected into a single group.
例如,下面的Students表的Grade列中有两行NULL值:
SELECT [Id] ,[Name] ,[Grade] ,[Age] FROM [dbo].[Students];
如果我们在查询中去GROUP BY列Grade,那么SQL Server会认为那两行NULL值是相等的,所以最后GROUP BY查询后,会将Grade列中的NULL值归类到一个GROUP:
SELECT Grade,COUNT(*) as [Count] FROM [dbo].[Students] GROUP BY Grade;