一.原数据:

SELECT inman,indate
FROM [fp_db].[dbo].[ppezhpbb]
WHERE indate > '2016-5-1' AND indate < '2016-6-1'
ORDER BY indate

PCB MS SQL 行转列-LMLPHP

二.转换后:

SELECT CONVERT(varchar(100), indate, 23) indate,
sum(case when inman='罗定鹏' then 1 else 0 end) 罗定鹏,
sum(case when inman='王波' then 1 else 0 end) 王波,
sum(case when inman='廖观长' then 1 else 0 end) 廖观长,
sum(case when inman='林星' then 1 else 0 end) 林星,
sum(case when inman='萧洪发' then 1 else 0 end) 萧洪发
FROM [fp_db].[dbo].[ppezhpbb]
WHERE indate > '2016-5-1' AND indate < '2016-6-1'
GROUP BY CONVERT(varchar(100), indate, 23)
ORDER BY indate

PCB MS SQL 行转列-LMLPHP

05-11 22:27