考勤数据结构表 zg_kq_data
dd 考勤日期
tt 考勤时间
考勤日期 考勤时间
2011-11-02 07:53:29
2011-11-02 12:01:30
... ...
... ...
每天刷最少4次卡,最多6次卡 这些刷卡时间原本以行显示 现在需要转列显示
dd duty1 duty2 duty3 duty4 duty5 duty6
2011-11-02 07:53:29 12:01:30 ... ... null null
你这个应该算不上是动态的吧。
CREATE TABLE #TEMP
(
[DATE] DATE,
[TIME] TIME
)
TRUNCATE TABLE #TEMP
INSERT INTO #TEMP
VALUES('2011-11-11','06:30:00'),('2011-11-11','08:30:00'),('2011-11-11','10:30:00'),('2011-11-11','12:30:00'),('2011-11-11','14:30:00'),('2011-11-11','16:30:00')
SELECT * FROM #TEMP
SELECT [DATE] ,
MAX(CASE WHEN [TIME] BETWEEN '06:00:00' AND '08:00:00' THEN [TIME] END) DUTY1,
MAX(CASE WHEN [TIME] BETWEEN '08:00:00' AND '10:00:00' THEN [TIME] END) DUTY2,
MAX(CASE WHEN [TIME] BETWEEN '10:00:00' AND '12:00:00' THEN [TIME]END) DUTY3,
MAX(CASE WHEN [TIME] BETWEEN '12:00:00' AND '14:00:00' THEN [TIME]END) DUTY4,
MAX(CASE WHEN [TIME] BETWEEN '14:00:00' AND '16:00:00' THEN [TIME]END) DUTY5,
MAX(CASE WHEN [TIME] BETWEEN '16:00:00' AND '18:00:00' THEN [TIME]END) DUTY6
FROM #TEMP GROUP BY [DATE]
支持!
SQLServer 2005及以上版本有提供 Pivot 方法,可以直接行转列的。
具体可以看下 http://www.cnblogs.com/peaceli/archive/2009/09/08/1561113.html 或者百度 Pivot.