- Create Test Table IF object_id (N ' T_Test ' , N ' the U- ' ) IS null the CREATE TABLE [ the dbo ] . [ T_Test ] ( [ ID ] int the IDENTITY ( . 1 , . 1 ) a PRIMARY Key the NOT NULL , [ Grouping ] VARCHAR ( 50 ) the NOT NULL , [ Ret ] varchar(10) null, [Chk] varchar(10) NULL ) GO --插入数据 insert into T_Test values('A', 1, 'XXX') insert into T_Test values('A', 2, 'XXX') insert into T_Test values('A', 2, 'YYY') insert into T_Test values('A', 2, null ) insert into T_Test values('A', 2, '' ) insert into T_Test values('A', null, 'cc' ) insert into T_Test values('A', '', 'cc' ) insert into T_Test values('B', 1, 'YYY') insert into T_Test values('B', 3, 'XXX') insert into T_Test values('B', 2, 'XXX') insert into T_Test values('B', 4, null ) insert into T_Test values('B', 5, '' ) insert into T_Test values('B', null, 'cc' ) insert into T_Test values('B', '', ' CC ' ) - . 1, according to the field of the packet Grouping Statistics field Ret (not empty) the number of different values, - 2. Grouping the chk field of the packet is not empty, Statistics field Ret (not empty) different the number of values, - using mainly functions do not count the number of statistical nULL, nullif function SELECT GROUPING , - count (DISTINCT Case When ISNULL (RET, '') <> '' End null the else the then RET), count ( DISTINCT NULLIF ( RET, '' )), COUNT ( DISTINCT Case When ISNULL (CHK, '' ) <> '' the then NULLIF (RET, '') Else zero end) from t_test group by GROUPING