我有一张桌子" AccessDetails"有字段
如何使用此信息在日期范围内获取滑动细节。这里swipeTime给出日期和时间,ReaderType给出IN / OUT。
employeeID employeeName designation department swipeLocation swipetime readerType
1157 IRINA NALOBINA Engineer Operations GF_LIFT LOBY FL_D3 2014-07-10 19:55:00.000 IN
1157 IRINA NALOBINA Engineer Operations GF_LIFT LOBY FL_D2 2014-07-10 20:00:00.000 OUT
1157 IRINA NALOBINA Engineer Operations GF_LIFT LOBY FL_D2 2014-07-10 20:10:00.000 IN
1157 IRINA NALOBINA Engineer Operations GF_LIFT LOBY FL_D2 2014-07-10 20:20:00.000 IN
1157 IRINA NALOBINA Engineer Operations GF_LIFT LOBY FL_D2 2014-07-10 20:35:00.000 OUT
1157 IRINA NALOBINA Engineer Operations GF_LIFT LOBY FL_D2 2014-07-11 19:00:00.000 IN
1157 IRINA NALOBINA Engineer Operations GF_LIFT LOBY FL_D2 2014-07-11 20:35:00.000 OUT
这里我有employeeId 1157有2天的进/出细节。如果我选择2014-07-11和2014-07-10两个日期,结果应为
1157 IRINA NALOBINA Engineer
Operations
GF_LIFT LOBY FL_D3(1st swipe in location)
2014-07-10 19:55:00.000(1st swipe in time)
GF_LIFT LOBY FL_D2(Last swipe out location)
2014-07-10 20:35:00.000(last swipe out time)
和
1157
IRINA NALOBINA
Engineer
Operations
GF_LIFT LOBY FL_D2(1st swipe in location)
2014-07-11 19:00:00.000(1st swipe in time)
GF_LIFT LOBY FL_D2(Last swipe out location)
2014-07-11 20:35:00.000(last swipe out time)
答案 0 :(得分:0)
样本表
CREATE TABLE #TEMP(employeeID INT,employeeName VARCHAR(100), designation VARCHAR(100), department VARCHAR(100), swipeLocation VARCHAR(100), swipetime DATETIME, readerType VARCHAR(3))
INSERT INTO #TEMP
SELECT 1157, 'IRINA NALOBINA', 'Engineer', 'Operations', 'GF_LIFT LOBY FL_D3', '2014-07-10 19:55:00.000', 'IN'
UNION ALL
SELECT 1157, 'IRINA NALOBINA', 'Engineer', 'Operations', 'GF_LIFT LOBY FL_D2', '2014-07-10 20:00:00.000', 'OUT'
UNION ALL
SELECT 1157, 'IRINA NALOBINA', 'Engineer', 'Operations', 'GF_LIFT LOBY FL_D2', '2014-07-10 20:10:00.000', 'IN'
UNION ALL
SELECT 1157, 'IRINA NALOBINA', 'Engineer', 'Operations', 'GF_LIFT LOBY FL_D2', '2014-07-10 20:20:00.000', 'IN'
UNION ALL
SELECT 1157, 'IRINA NALOBINA', 'Engineer', 'Operations', 'GF_LIFT LOBY FL_D2', '2014-07-10 20:35:00.000', 'OUT'
UNION ALL
SELECT 1157, 'IRINA NALOBINA', 'Engineer', 'Operations', 'GF_LIFT LOBY FL_D2', '2014-07-11 19:00:00.000', 'IN'
UNION ALL
SELECT 1157, 'IRINA NALOBINA', 'Engineer', 'Operations', 'GF_LIFT LOBY FL_D2', '2014-07-11 20:35:00.000', 'OUT'
我们需要先找出每个日期的第一次和最后一次刷卡时间,然后找到它们的位置。我在查询中详细编写了逻辑。
DECLARE @FROMDATE DATE='2014-07-10'
DECLARE @TODATE DATE='2014-07-11'
;WITH CTE AS
(
-- Here we find out the 1st swipe in time and last swipe out time for an employee for each date
SELECT
employeeID,employeeName,designation,department,
swipeLocation,
swipetime,
MIN(CASE WHEN readerType='IN' THEN swipetime END) OVER(PARTITION BY CAST(swipetime AS DATE)) MINDATE,
MAX(CASE WHEN readerType='OUT' THEN swipetime END) OVER(PARTITION BY CAST(swipetime AS DATE)) MAXDATE
,readerType
FROM #TEMP
WHERE CAST(SWIPETIME AS DATE) BETWEEN @FROMDATE AND @TODATE
)
,CTE2 AS
(
-- We format th date column and location column with string
SELECT ROW_NUMBER() OVER(ORDER BY MINDATE)RNO,
CAST(employeeID AS VARCHAR(30))employeeID,employeeName,designation,department,
swipeLocation + (CASE WHEN readerType='IN' THEN '(1st swipe in location)' ELSE '(Last swipe out location)' END) swipeLocation,
CONVERT(VARCHAR(19),MINDATE, 121) + '(1st swipe in time)' MINDATE,
CONVERT(VARCHAR(19),MAXDATE, 121) + '(Last swipe out time)' MAXDATE,
readerType
FROM CTE
WHERE swipetime=MINDATE OR swipetime=MAXDATE
)
,CTE3 AS
(
-- Find out the 1st and last swipe location for 1st and last time on that date and keep in seperate columns
SELECT C2.RNO,C2.employeeID,C2.employeeName,C2.designation,C2.department,C2.swipeLocation INLOC,C2.MINDATE,C2.MAXDATE,C2.readerType readerTypeIN,
C3.swipeLocation OUTLOC,C3.readerType readerTypeOUT
FROM CTE2 C2
LEFT JOIN CTE2 C3 ON C2.RNO = C3.RNO-1
)
-- We bring all the values to single column using CROSS APPLY
-- Since we get next record details in the same line in CTE3, we omit each row which are multiples of 2
SELECT CAST(COL AS NVARCHAR(300))COL
FROM CTE3
CROSS APPLY(VALUES(employeeID),(employeeName),(designation),(department),(INLOC),(MINDATE),(OUTLOC),(MAXDATE)) CS (COL)
WHERE RNO % 2 <> 0
<强> RESULT 强>