PCB MS SQL 行转列

 一.原数据:

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

 

二.转换后:

复制代码
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
复制代码

 

posted @   pcbren  阅读(248)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示