1,Group by 根据某个字段排序
select Department,count(*) FROM [PPMG].[dbo].[UnConViolation] group by Department
结果如下:
2,Group by与所传参数联合使用
USE PPMGSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOAlter PROCEDURE UnConViolationTimes @type nvarchar(50)ASBEGIN declare @sql nvarchar(1000) if @type is not null begin set @sql='select '+ @type+' as Name,count(*) as Count FROM [PPMG].[dbo].[UnConViolation] group by '+@type print @sql exec(@sql) end ENDGO
exec UnConViolationTimes 'Department'
结果如下
3,Group by 后台用法
public ListGetUnConViolationTimes(string type) { var UnConViolationList = UnConViolationRepository.Query(); var ttList = UnConViolationList.GroupBy(m => new { m.Department}).Select(m => new ViolationTimesDto() { Name=m.Key.Department, Count=m.Count() }).ToList(); return ttList; }
结果如下