您好,登錄后才能下訂單哦!
字符的合并,更確切的說是字段數據的合并吧。現在很多公司,尤其是工廠,上下班都要刷卡。假如有以下的數據庫表,記錄著員工上下班的刷卡記錄。但是這樣直接導出或顯示,不太好看,不夠直觀。下面實現把每個員工每天的刷卡時間橫向顯示。
Figure-1: 打卡記錄
插入測試數據:
IF OBJECT_ID(N'dbo.CARD_RECORD','U') IS NOT NULL BEGIN DROP TABLE dbo.CARD_RECORD; END GO CREATE TABLE dbo.CARD_RECORD ( id_ INT IDENTITY(1,1) NOT NULL PRIMARY KEY, card_id INT NOT NULL, swipe_date DATETIME NOT NULL ) GO INSERT INTO dbo.CARD_RECORD (card_id,swipe_date) SELECT '10001','2015-06-01 08:21' UNION ALL SELECT '10002','2015-06-01 08:22' UNION ALL SELECT '10001','2015-06-01 12:00' UNION ALL SELECT '10002','2015-06-01 12:01' UNION ALL SELECT '10001','2015-06-01 13:00' UNION ALL SELECT '10002','2015-06-01 13:01' UNION ALL SELECT '10002','2015-06-01 18:05' UNION ALL SELECT '10001','2015-06-01 18:12' UNION ALL SELECT '10002','2015-06-02 08:31' UNION ALL SELECT '10001','2015-06-02 08:42' UNION ALL SELECT '10001','2015-06-02 12:10' UNION ALL SELECT '10002','2015-06-02 12:11' UNION ALL SELECT '10001','2015-06-02 13:00' UNION ALL SELECT '10002','2015-06-02 13:11' UNION ALL SELECT '10001','2015-06-02 18:05' UNION ALL SELECT '10002','2015-06-02 18:12' UNION ALL SELECT '10002','2015-06-02 19:34' UNION ALL SELECT '10001','2015-06-03 08:36' UNION ALL SELECT '10002','2015-06-03 08:40' UNION ALL SELECT '10001','2015-06-03 12:20' UNION ALL SELECT '10002','2015-06-03 12:20' UNION ALL SELECT '10001','2015-06-03 12:55' UNION ALL SELECT '10002','2015-06-03 12:56' UNION ALL SELECT '10001','2015-06-03 18:05' GO
Code-1: 插入測試數據
創建字符/字段數據合并的函數:
IF OBJECT_ID(N'dbo.fn_time_list') IS NOT NULL BEGIN DROP FUNCTION dbo.fn_time_list END GO CREATE FUNCTION dbo.fn_time_list ( @date DATETIME, @card_id NVARCHAR(100) ) RETURNS NVARCHAR(MAX) AS BEGIN DECLARE @char NVARCHAR(MAX) DECLARE @date2 DATE SET @char = '' SET @date = CAST(@date AS DATE) SET @date2 = DATEADD(DAY,1,CAST(@date AS DATE)) SELECT @char = @char + CONVERT(CHAR(5),swipe_date,108) + ',' FROM CARD_RECORD WHERE (swipe_date >= @date AND swipe_date < @date2) AND card_id=@card_id ORDER BY swipe_date ASC SET @char = CASE WHEN @char = '' THEN '' ELSE SUBSTRING(@char,1,LEN(@char)-1) END RETURN (@char) END GO
Code-2: 字符/字段數據合并函數
最后,把數據轉換一下,展示:
SELECT DISTINCT card_id, CONVERT(char(10), swipe_date,23) AS swipe_date, dbo.fn_time_list(swipe_date,card_id) AS time_list2 FROM dbo.CARD_RECORD ORDER BY card_id ASC,swipe_date ASC; GO
Code-3: 轉換顯示
執行結果:
Figure-2: 轉換后顯示的效果
這里,還可以把豎向的刷卡時間,每個時間占一個字段,橫向顯示,方便統計等。不過會有刷卡次數的限制,一般來說,一天預留十次刷卡記錄應該是足夠了。
WITH CTE3 AS ( SELECT card_id ,CONVERT(CHAR(10),swipe_date,23) AS swipe_date ,CONVERT(CHAR(5),swipe_date,108) AS swipe_time FROM CARD_RECORD ) ,CTE4 AS ( SELECT ROW_NUMBER() OVER (PARTITION BY card_id,swipe_date ORDER BY card_id ASC,swipe_date ASC,swipe_time ASC) AS row_no ,card_id ,swipe_date ,swipe_time FROM CTE3 ) SELECT card_id ,swipe_date ,MAX(CASE WHEN row_no = 1 THEN swipe_time ELSE '' END) AS time1 ,MAX(CASE WHEN row_no = 2 THEN swipe_time ELSE '' END) AS time2 ,MAX(CASE WHEN row_no = 3 THEN swipe_time ELSE '' END) AS time3 ,MAX(CASE WHEN row_no = 4 THEN swipe_time ELSE '' END) AS time4 ,MAX(CASE WHEN row_no = 5 THEN swipe_time ELSE '' END) AS time5 ,MAX(CASE WHEN row_no = 6 THEN swipe_time ELSE '' END) AS time6 ,MAX(CASE WHEN row_no = 7 THEN swipe_time ELSE '' END) AS time7 ,MAX(CASE WHEN row_no = 8 THEN swipe_time ELSE '' END) AS time8 ,MAX(CASE WHEN row_no = 9 THEN swipe_time ELSE '' END) AS time9 ,MAX(CASE WHEN row_no = 10 THEN swipe_time ELSE '' END) AS time10 FROM CTE4 GROUP BY card_id,swipe_date ORDER BY card_id ASC,swipe_date ASC; GO
Code-4: 轉為每個時間占用一個字段
最終的效果:
Figure-3: 最終顯示的效果
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。