根据用户id查询所有时间
1 CREATE TABLE [dbo].[esService_WeiXinNew_Check] 2 ( 3 [fldGuid] [uniqueidentifier] NOT NULL PRIMARY KEY, 4 [fldCheckTime] [smalldatetime] NOT NULL, 5 [fldInfoGuid] [uniqueidentifier] NOT NULL 6 ) 7 GO 8 INSERT INTO [esService_WeiXinNew_Check] 9 SELECT ‘0F5AF386-9D76-4D1A-8E49-97968C46E778‘,‘2020-02-28 15:17:00‘,‘7DF70568-6400-4C1F-B28D-426F0F98D8D2‘ 10 UNION ALL 11 SELECT ‘E76F4124-C2F1-4B41-926D-CFC4126DF36B‘,‘2020-02-29 10:16:00‘,‘7DF70568-6400-4C1F-B28D-426F0F98D8D2‘ 12 UNION ALL 13 SELECT ‘09869C2D-13EA-4CEC-99BF-57EFE00DFF43‘,‘2020-03-15 10:16:00‘,‘7DF70568-6400-4C1F-B28D-426F0F98D8D2‘ 14 UNION ALL 15 SELECT ‘2EEF2EF6-C6E5-4187-B052-8BA2FFC17E6D‘,‘2020-03-16 10:16:00‘,‘7DF70568-6400-4C1F-B28D-426F0F98D8D2‘ 16 UNION ALL 17 SELECT ‘5F9C3BB2-F0A3-4B77-9C46-E92358921308‘,‘2020-03-17 10:16:00‘,‘7DF70568-6400-4C1F-B28D-426F0F98D8D2‘ 18 UNION ALL 19 SELECT ‘A37A4F73-8EC3-46E0-B99F-E042B2112C21‘,‘2020-03-18 10:16:00‘,‘7DF70568-6400-4C1F-B28D-426F0F98D8D2‘ 20 UNION ALL 21 SELECT ‘888DDDE6-3DD5-4298-AC49-9F8CBD8C0DCC‘,‘2020-03-19 10:16:00‘,‘7DF70568-6400-4C1F-B28D-426F0F98D8D2‘ 22 UNION ALL 23 SELECT ‘C6DACA30-65F7-40D9-805A-3C90B0BA0D07‘,‘2020-03-20 10:16:00‘,‘7DF70568-6400-4C1F-B28D-426F0F98D8D2‘ 24 25 26 DECLARE @fldInfoGuid UNIQUEIDENTIFIER = ‘7DF70568-6400-4C1F-B28D-426F0F98D8D2‘; 27 SELECT fldGuid, fldCheckTime, fldInfoGuid FROM esService_WeiXinNew_Check WHERE fldInfoGuid = @fldInfoGuid ORDER BY fldCheckTime 28 29 SELECT t.fldCheckTime,t1.fldCheckTime,DATEDIFF(DAY,t.fldCheckTime,t1.fldCheckTime)+1 AS num FROM esService_WeiXinNew_Check t 30 OUTER APPLY( 31 SELECT MIN(a.fldCheckTime) AS fldCheckTime FROM esService_WeiXinNew_Check a WHERE a.fldInfoGuid = t.fldInfoGuid AND NOT EXISTS(SELECT TOP 1 1 FROM esService_WeiXinNew_Check WHERE fldInfoGuid = a.fldInfoGuid AND CONVERT(NVARCHAR(10),fldCheckTime,120) = CONVERT(NVARCHAR(10),DATEADD(DAY, 1, a.fldCheckTime),120)) AND a.fldCheckTime > t.fldCheckTime)t1 32 WHERE t.fldInfoGuid = @fldInfoGuid 33 AND NOT EXISTS(SELECT * FROM esService_WeiXinNew_Check WHERE fldInfoGuid = t.fldInfoGuid AND CONVERT(NVARCHAR(10),fldCheckTime,120) = CONVERT(NVARCHAR(10),DATEADD(DAY, -1, t.fldCheckTime),120));
根据用户id及时间点查询
DECLARE @fldInfoGuid UNIQUEIDENTIFIER = ‘7DF70568-6400-4C1F-B28D-426F0F98D8D2‘; DECLARE @fldCheckTime SMALLDATETIME = ‘2020-03-20‘ SELECT fldGuid, fldCheckTime, fldInfoGuid FROM esService_WeiXinNew_Check WHERE fldInfoGuid = @fldInfoGuid ORDER BY fldCheckTime SELECT t.fldCheckTime,t1.fldCheckTime,DATEDIFF(DAY,t.fldCheckTime,t1.fldCheckTime)+1 AS num FROM esService_WeiXinNew_Check t OUTER APPLY( SELECT MIN(a.fldCheckTime) AS fldCheckTime FROM esService_WeiXinNew_Check a WHERE a.fldInfoGuid = t.fldInfoGuid AND NOT EXISTS(SELECT TOP 1 1 FROM esService_WeiXinNew_Check WHERE fldInfoGuid = a.fldInfoGuid AND CONVERT(NVARCHAR(10),fldCheckTime,120) = CONVERT(NVARCHAR(10),DATEADD(DAY, 1, a.fldCheckTime),120)) AND a.fldCheckTime > t.fldCheckTime)t1 WHERE t.fldInfoGuid = @fldInfoGuid AND CONVERT(NVARCHAR(10),t1.fldCheckTime,120) = CONVERT(NVARCHAR(10),@fldCheckTime,120) AND NOT EXISTS(SELECT * FROM esService_WeiXinNew_Check WHERE fldInfoGuid = t.fldInfoGuid AND CONVERT(NVARCHAR(10),fldCheckTime,120) = CONVERT(NVARCHAR(10),DATEADD(DAY, -1, t.fldCheckTime),120));