SQL Server 实现数据的行列转换 并且分组

来源:互联网 发布:热情高涨读音 知乎 编辑:程序博客网 时间:2024/06/05 15:19

SQL Server 实现数据的行列转换 并且分组


select t1.UserNo,t1.Pad_Num,t2.EmployeeName,count(t1.Pad_Num) AS cnt
 into #temp_pad from tb_pad as t1
left join tb_Employee as t2 on t2.EmployeeNo = t1.UserNo
where CONVERT(CHAR(10), T1.RecordDate, 120)  between ''' + @beginDate + ''' and ''' + @endDate + '''
group by t1.UserNo, t1.Pad_Num,t2.EmployeeName


select EmployeeName AS ''姓名'',
 max(case when Pad_Num= ''1'' then cnt else 0 end) 非常满意,
 max(case when Pad_Num= ''7'' then cnt else 0 end) 满意,
 max(case when Pad_Num= ''2'' then cnt else 0 end) 基本满意,
 max(case when Pad_Num= ''3'' then cnt else 0 end) 不满意
from #temp_pad 
group by EmployeeName

0 0
原创粉丝点击