我有两个表,一个是包含用户详细信息的Tab_Users
,另一个是包含其卡条目的Tab_CardEntry
。我需要每天为每个用户获取最短和最长的卡片进入时间。
这是我使用的SQL查询
SELECT
A.Name,
ISNULL(CONVERT(VARCHAR, MIN(B.DATED), 108), 'OFF') LOGEDIN,
ISNULL(CONVERT(VARCHAR, MAX(B.DATED), 108), 'OFF') LOGEDOUT
FROM TAB_USERS A
LEFT OUTER JOIN (
SELECT
USERNAME,
DATED
FROM TAB_CARDENTRY
WHERE CONVERT(VARCHAR, DATED, 106) = CONVERT(VARCHAR, '02 Jan 2014', 106)
) B ON A.USERNAME = B.USERNAME
GROUP BY A.Name
ORDER BY 1
我需要这种类型的输出
答案 0 :(得分:0)
以下是您可以尝试的存储过程
CREATE PROCEDURE usp_GetTimes @StartDate DATETIME, @EndDate DATETIME = NULL
AS
BEGIN
--Make sure that only 31 days of data is used otherwise the temp table will be too big
IF @EndDate IS NULL OR DATEDIFF(dd, @StartDate, @EndDate) > 31
SET @EndDate = DATEADD(dd, 31, @StartDate)
SELECT UserID, CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, DateTimeIn))) ForDate, DateTimeIn, DateTimeOut
INTO #Times
FROM
(
SELECT DISTINCT MIN(a.DATED) OVER (PARTITION BY a.USERNAME, CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, DATED)))) DateTimeIn,
MAX(a.DATED) OVER (PARTITION BY a.USERNAME, CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, DATED)))) DateTimeOut,
USERNAME AS UserID
FROM TAB_CARDENTRY a
WHERE DATED BETWEEN @StartDate and @EndDate + 1
) a
SELECT DISTINCT ForDate INTO #Dates FROM #Times
--Select * from #Times order by 1
--Select * from #Dates
DECLARE @SQLstring VARCHAR(MAX)
SET @SQLstring = 'CREATE TABLE ##Test (UserID varchar(10), '
DECLARE @ForDate DATETIME
WHILE 0 < (SELECT COUNT(*) FROM #Dates)
BEGIN
SET ROWCOUNT 1
SELECT @ForDate = ForDate FROM #Dates order by ForDate
SET ROWCOUNT 0
SET @SQLstring = @SQLstring + '[Entry ' + CONVERT(VARCHAR(12), @ForDate, 106) + '] datetime, [Exit ' + CONVERT(VARCHAR(12), @ForDate, 106) + '] datetime,' + CHAR(13) + CHAR(10)
DELETE #Dates WHERE ForDate = @ForDate
END
SET @SQLstring = SUBSTRING(@SQLSTRING, 1, LEN(@SQLSTRING) - 3) + ')'
EXEC (@SQLstring)
CREATE INDEX idx1 on ##Test(UserID)
DECLARE @UserID VARCHAR(10),
@DateIn DATETIME,
@DateOut DATETIME
WHILE 0 < (SELECT COUNT(*) FROM #Times)
BEGIN
SET ROWCOUNT 1
SELECT @UserID = UserID, @DateIn = DateTimeIn, @DateOut = DateTimeOut, @ForDate = ForDate from #Times
SET ROWCOUNT 0
SET @SQLString = 'IF NOT EXISTS(SELECT 1 FROM ##Test WHERE UserID = ''' + @UserID + ''')' + CHAR(13) + CHAR(10) +
' INSERT ##Test (UserID, [Entry ' + CONVERT(VARCHAR(12), @ForDate, 106) + '], [Exit ' + CONVERT(VARCHAR(12), @ForDate, 106) + ']) ' +
' SELECT ''' + @UserID + ''', ''' + CONVERT(VARCHAR(20), @DateIn, 113) + ''', ''' + CONVERT(VARCHAR(20), @DateOut, 113) + '''' + CHAR(13) + CHAR(10) +
'ELSE' + CHAR(13) + CHAR(10) +
' UPDATE ##Test SET [Entry ' + CONVERT(VARCHAR(12), @ForDate, 106) + '] = ''' + CONVERT(VARCHAR(20), @DateIn, 113) + ''', ' + CHAR(13) + CHAR(10) +
' [Exit ' + CONVERT(VARCHAR(12), @ForDate, 106) + '] = ''' + CONVERT(VARCHAR(20), @DateOut, 113) + ''' WHERE UserID = ''' + @UserID + ''''
EXEC (@SQLstring)
DELETE #Times WHERE UserID = @UserID AND ForDate = @ForDate
END
SELECT * FROM ##Test ORDER BY 1
DROP TABLE ##Test
END
修改强>
我再次查看了查询,您可能不喜欢输出,因此您可以将SP的一部分更改为:
创建临时表的部分
WHILE 0 < (SELECT COUNT(*) FROM #Dates)
BEGIN
SET ROWCOUNT 1
SELECT @ForDate = ForDate FROM #Dates order by ForDate
SET ROWCOUNT 0
SET @SQLstring = @SQLstring + '[Entry ' + CONVERT(VARCHAR(12), @ForDate, 106) + '] varchar(20), [Exit ' + CONVERT(VARCHAR(12), @ForDate, 106) + '] varchar(20),' + CHAR(13) + CHAR(10)
DELETE #Dates WHERE ForDate = @ForDate
END
将数据插入表中的部分
WHILE 0 < (SELECT COUNT(*) FROM #Times)
BEGIN
SET ROWCOUNT 1
SELECT @UserID = UserID, @DateIn = DateTimeIn, @DateOut = DateTimeOut, @ForDate = ForDate from #Times
SET ROWCOUNT 0
SET @SQLString = 'IF NOT EXISTS(SELECT 1 FROM ##Test WHERE UserID = ''' + @UserID + ''')' + CHAR(13) + CHAR(10) +
' INSERT ##Test (UserID, [Entry ' + CONVERT(VARCHAR(12), @ForDate, 106) + '], [Exit ' + CONVERT(VARCHAR(12), @ForDate, 106) + ']) ' +
' SELECT ''' + @UserID + ''', ''' + CONVERT(VARCHAR(20), ISNULL(@DateIn, '''OFF''', 108) + ''', ''' + CONVERT(VARCHAR(20), ISNULL(@DateOut, '''OFF'''), 108) + '''' + CHAR(13) + CHAR(10) +
'ELSE' + CHAR(13) + CHAR(10) +
' UPDATE ##Test SET [Entry ' + CONVERT(VARCHAR(12), @ForDate, 106) + '] = ''' + CONVERT(VARCHAR(20), ISNULL(@DateIn, '''OFF'''), 108) + ''', ' + CHAR(13) + CHAR(10) +
' [Exit ' + CONVERT(VARCHAR(12), @ForDate, 106) + '] = ''' + CONVERT(VARCHAR(20), ISNULL(@DateOut, '''OFF''', 108) + ''' WHERE UserID = ''' + @UserID + ''''
EXEC (@SQLstring)
DELETE #Times WHERE UserID = @UserID AND ForDate = @ForDate
END