国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

字符/字段數(shù)據(jù)的合并

2024-07-21 02:46:41
字體:
供稿:網(wǎng)友
字符/字段數(shù)據(jù)的合并

前兩篇文章是關(guān)于字符串分割的一些經(jīng)驗,今天來談下字符的合并,更確切的說是字段數(shù)據(jù)的合并吧。現(xiàn)在很多公司,尤其是工廠,上下班都要刷卡。假如有以下的數(shù)據(jù)庫表,記錄著員工上下班的刷卡記錄。但是這樣直接導(dǎo)出或顯示,不太好看,不夠直觀。下面實現(xiàn)把每個員工每天的刷卡時間橫向顯示。

插入測試數(shù)據(jù):

 1 CREATE TABLE CARD_RECORD ( 2     id_ INT IDENTITY(1,1) NOT NULL PRIMARY KEY, 3     card_id INT NOT NULL, 4     swipe_date DATETIME NOT NULL 5 ) 6 GO 7  8 INSERT INTO CARD_RECORD (card_id,swipe_date) 9 SELECT '10001','2015-06-01 08:21'10 UNION ALL11 SELECT '10002','2015-06-01 08:22'12 UNION ALL13 SELECT '10001','2015-06-01 12:00'14 UNION ALL15 SELECT '10002','2015-06-01 12:01'16 UNION ALL17 SELECT '10001','2015-06-01 13:00'18 UNION ALL19 SELECT '10002','2015-06-01 13:01'20 UNION ALL21 SELECT '10002','2015-06-01 18:05'22 UNION ALL23 SELECT '10001','2015-06-01 18:12'24 UNION ALL25 SELECT '10002','2015-06-02 08:31'26 UNION ALL27 SELECT '10001','2015-06-02 08:42'28 UNION ALL29 SELECT '10001','2015-06-02 12:10'30 UNION ALL31 SELECT '10002','2015-06-02 12:11'32 UNION ALL33 SELECT '10001','2015-06-02 13:00'34 UNION ALL35 SELECT '10002','2015-06-02 13:11'36 UNION ALL37 SELECT '10001','2015-06-02 18:05'38 UNION ALL39 SELECT '10002','2015-06-02 18:12'40 UNION ALL41 SELECT '10002','2015-06-02 19:34'42 UNION ALL43 SELECT '10001','2015-06-03 08:36'44 UNION ALL45 SELECT '10002','2015-06-03 08:40'46 UNION ALL47 SELECT '10001','2015-06-03 12:20'48 UNION ALL49 SELECT '10002','2015-06-03 12:20'50 UNION ALL51 SELECT '10001','2015-06-03 12:55'52 UNION ALL53 SELECT '10002','2015-06-03 12:56'54 UNION ALL55 SELECT '10001','2015-06-03 18:05'56 GO
View Code

創(chuàng)建字符/字段數(shù)據(jù)合并的函數(shù):

 1 IF OBJECT_ID(N'fn_time_list') IS NOT NULL 2 BEGIN 3     DROP FUNCTION fn_time_list 4 END 5 GO 6  7 CREATE FUNCTION fn_time_list (@date DATETIME,@card_id NVARCHAR(100)) 8 RETURNS NVARCHAR(MAX) 9 AS 10 BEGIN11     DECLARE @char NVARCHAR(MAX)12     DECLARE @date2 DATE13     SET @char = ''14     SET @date = CAST(@date AS DATE)15     SET @date2 = DATEADD(DAY,1,CAST(@date AS DATE))16     17     SELECT @char = @char + CONVERT(CHAR(5),swipe_date,108) + ',' FROM CARD_RECORD 18     WHERE (swipe_date >= @date AND swipe_date < @date2) AND card_id=@card_id19     ORDER BY swipe_date ASC20     21     SET @char = CASE WHEN @char = '' THEN '' ELSE SUBSTRING(@char,1,LEN(@char)-1) END22     23     RETURN (@char)24 END25 GO

最后,把數(shù)據(jù)轉(zhuǎn)換一下,展示:

1 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 CARD_RECORD 3 ORDER BY card_id ASC,swipe_date ASC4 GO

執(zhí)行結(jié)果:

這里,還可以把豎向的刷卡時間,每個時間占一個字段,橫向顯示。不過會有刷卡次數(shù)的限制,一般來說,一天預(yù)留十次刷卡記錄應(yīng)該是足夠了。

 1 WITH CTE1 AS ( 2     SELECT  3      card_id 4     ,CONVERT(CHAR(10),swipe_date,23) AS swipe_date 5     ,CONVERT(CHAR(5),swipe_date,108) AS swipe_time 6     FROM CARD_RECORD 7 ) 8  9 ,CTE2 AS (10     SELECT 11      ROW_NUMBER() OVER (PARTITION BY card_id,swipe_date ORDER BY card_id ASC,swipe_date ASC,swipe_time ASC) AS row_no12     ,card_id13     ,swipe_date14     ,swipe_time15     FROM CTE116 )17 18 SELECT 19  t1.card_id20 ,t1.swipe_date21 ,t1.swipe_time AS time122 ,ISNULL(t2.swipe_time,'') AS time223 ,ISNULL(t3.swipe_time,'') AS time324 ,ISNULL(t4.swipe_time,'') AS time425 ,ISNULL(t5.swipe_time,'') AS time526 ,ISNULL(t6.swipe_time,'') AS time627 ,ISNULL(t7.swipe_time,'') AS time728 ,ISNULL(t8.swipe_time,'') AS time829 ,ISNULL(t9.swipe_time,'') AS time930 ,ISNULL(t10.swipe_time,'') AS time1031 32 FROM CTE2 AS t133 LEFT JOIN CTE2 AS t2 ON t2.card_id = t1.card_id AND t2.swipe_date = t1.swipe_date AND t2.row_no = 234 LEFT JOIN CTE2 AS t3 ON t3.card_id = t1.card_id AND t3.swipe_date = t1.swipe_date AND t3.row_no = 335 LEFT JOIN CTE2 AS t4 ON t4.card_id = t1.card_id AND t4.swipe_date = t1.swipe_date AND t4.row_no = 436 LEFT JOIN CTE2 AS t5 ON t5.card_id = t1.card_id AND t5.swipe_date = t1.swipe_date AND t5.row_no = 537 LEFT JOIN CTE2 AS t6 ON t6.card_id = t1.card_id AND t6.swipe_date = t1.swipe_date AND t6.row_no = 638 LEFT JOIN CTE2 AS t7 ON t7.card_id = t1.card_id AND t7.swipe_date = t1.swipe_date AND t7.row_no = 739 LEFT JOIN CTE2 AS t8 ON t8.card_id = t1.card_id AND t8.swipe_date = t1.swipe_date AND t8.row_no = 840 LEFT JOIN CTE2 AS t9 ON t9.card_id = t1.card_id AND t9.swipe_date = t1.swipe_date AND t9.row_no = 941 LEFT JOIN CTE2 AS t10 ON t10.card_id = t1.card_id AND t10.swipe_date = t1.swipe_date AND t10.row_no = 1042 43 WHERE t1.row_no = 144 ORDER BY t1.card_id ASC,t1.swipe_date ASC45 GO

最后的效果:

======= 以下代碼減少了CTE虛擬表LEFT JOIN的次數(shù),效率更高。修改于2015-06-24 19:22 ===========

 1 ;WITH CTE3 AS ( 2     SELECT  3      card_id 4     ,CONVERT(CHAR(10),swipe_date,23) AS swipe_date 5     ,CONVERT(CHAR(5),swipe_date,108) AS swipe_time 6     FROM CARD_RECORD 7 ) 8  9 ,CTE4 AS (10     SELECT 11      ROW_NUMBER() OVER (PARTITION BY card_id,swipe_date ORDER BY card_id ASC,swipe_date ASC,swipe_time ASC) AS row_no12     ,card_id13     ,swipe_date14     ,swipe_time15     FROM CTE316 )17 18 SELECT 19  card_id20 ,swipe_date21 ,MAX(CASE WHEN row_no = 1 THEN swipe_time ELSE '' END) AS time122 ,MAX(CASE WHEN row_no = 2 THEN swipe_time ELSE '' END) AS time223 ,MAX(CASE WHEN row_no = 3 THEN swipe_time ELSE '' END) AS time324 ,MAX(CASE WHEN row_no = 4 THEN swipe_time ELSE '' END) AS time425 ,MAX(CASE WHEN row_no = 5 THEN swipe_time ELSE '' END) AS time526 ,MAX(CASE WHEN row_no = 6 THEN swipe_time ELSE '' END) AS time627 ,MAX(CASE WHEN row_no = 7 THEN swipe_time ELSE '' END) AS time728 ,MAX(CASE WHEN row_no = 8 THEN swipe_time ELSE '' END) AS time829 ,MAX(CASE WHEN row_no = 9 THEN swipe_time ELSE '' END) AS time930 ,MAX(CASE WHEN row_no = 10 THEN swipe_time ELSE '' END) AS time1031 32 FROM CTE433 GROUP BY card_id,swipe_date34 ORDER BY card_id ASC,swipe_date ASC35 36 GO


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宕昌县| 聊城市| 四平市| 霞浦县| 甘泉县| 永善县| 吐鲁番市| 唐海县| 南皮县| 财经| 三都| 蓬溪县| 牙克石市| 江城| 抚宁县| 股票| 灵武市| 安化县| 耿马| 攀枝花市| 湟中县| 玛沁县| 衡南县| 嵩明县| 涪陵区| 武汉市| 辽宁省| 西昌市| 临汾市| 科尔| 博白县| 克什克腾旗| 常熟市| 桑日县| 郧西县| 奈曼旗| 西乌珠穆沁旗| 饶平县| 通山县| 伊川县| 吴江市|