我让查询显示TA值,结果将显示如下
SELECT convert(varchar(10),DATEADD(second, dbo.TB_EVENT_LOG.nDateTime, '19700101 00:00:00'),103) as [date] ,left(convert(varchar(10),DATEADD(second, dbo.TB_EVENT_LOG.nDateTime, '19700101 00:00:00'),108),5) as [time],
CASE WHEN dbo.TB_EVENT_LOG.[nReaderIdn] in(539577044) THEN 'F1' WHEN dbo.TB_EVENT_LOG.[nReaderIdn] in(539577051) THEN 'F2' END AS FunctionKey,TB_USER_CUSTOMINFO.sFieldValue8 as EMPLOYEEID
FROM dbo.TB_EVENT_LOG INNER JOIN dbo.TB_USER ON dbo.TB_EVENT_LOG.nUserID = dbo.TB_USER.sUserID INNER JOIN dbo.TB_USER_CUSTOMINFO ON dbo.TB_USER.nUserIdn = dbo.TB_USER_CUSTOMINFO.nUserIdn
where nDateTime > datediff(second, '19700101 00:00:00', DATEADD(DAY, -4, getdate())) and nDateTime < datediff(second, '19700101 00:00:00', getdate())
╔════════════╤═══════╤═════════════╤════════════╗
║ date │ time │ FunctionKey │ EMPLOYEEID ║
╠════════════╪═══════╪═════════════╪════════════╣
║ 21/02/2019 │ 08:37 AM │ F1 │ 19564 ║
╟────────────┼───────┼─────────────┼────────────╢
║ 21/02/2019 │ 04:39 PM │ F2 │ 19564 ║
╟────────────┼───────┼─────────────┼────────────╢
║ 21/02/2019 │ 04:56 PM │ F1 │ 19564 ║
╟────────────┼───────┼─────────────┼────────────╢
║ 21/02/2019 │ 05:49 PM│ F2 │ 19564 ║
╟────────────┼───────┼─────────────┼────────────╢
║ 21/02/2019 │ 05:53 PM │ F1 │ 19564 ║
╟────────────┼───────┼─────────────┼────────────╢
║ 21/02/2019 │ 06:06 PM│ F2 │ 19564 ║
╟────────────┼───────┼─────────────┼────────────╢
║ 21/02/2019 │ 01:26 PM│ F1 │ 19564 ║
╟────────────┼───────┼─────────────┼────────────╢
║ 21/02/2019 │ 01:32 PM│F1 │ 19564 ║
╟────────────┼───────┼─────────────┼────────────╢
║ 21/02/2019 │ 01:33 PM│ F2 │ 19564 ║
╟────────────┼───────┼─────────────┼────────────╢
║ 21/02/2019 │ 01:45 PM│ F1 │ 19564 ║
╟────────────┼───────┼─────────────┼────────────╢
║ 21/02/2019 │ 03:00 PM│ F2 │ 19564 ║
╟────────────┼───────┼─────────────┼────────────╢
║ 21/02/2019 │ 04:38 PM│ F1 │ 19564 ║
╚════════════╧═══════╧═════════════╧════════════╝
即使每天同一位用户不止一次,结果也会显示所有出勤时间。
如果任何人可以帮助我,请仅显示每个日期的最小值F1和最大值F2,员工编号
╔════════════╤═══════╤═════════════╤════════════╗
║ date │ time │ FunctionKey │ EMPLOYEEID ║
╠════════════╪═══════╪═════════════╪════════════╣
║ 21/02/2019 │ 08:37 AM│ F1 │ 19564 ║
╟────────────┼───────┼─────────────┼────────────╢
║ 21/02/2019 │ 06:06 PM│ F2 │ 19564 ║
╚════════════╧═══════╧═════════════╧════════════╝
答案 0 :(得分:0)
为了在同一列显示结果,我做了一些更改,
setValue()
此处结果将显示每个EmployeeID的F1的最小时间值和F2的最大时间值,日期在同一列,同样,我不确定您使用的是哪种时间类型,也许您可以将当前格式转换为24小时格式化然后排序。
答案 1 :(得分:0)
您应该在此处使用UNION来获得两个结果,并且在计算MIN和MAX时,它应位于原始日期/时间列,而不是转换后的字符串
类似的事情,但是得到了极大的简化
SELECT CONVERT(...dbo.TB_EVENT_LOG.nDateTime...) AS [date],
CONVERT(..., MIN(dbo.TB_EVENT_LOG.nDateTime), ...) AS [Time], empId, 'F1'
WHERE funcKey = 'F1'
GROUP BY CONVERT(...dbo.TB_EVENT_LOG.nDateTime...), empId
UNION ALL
SELECT CONVERT(...dbo.TB_EVENT_LOG.nDateTime...) AS [date],
CONVERT(..., MAX(dbo.TB_EVENT_LOG.nDateTime), ...) AS [Time], empId, 'F2'
WHERE funcKey = 'F2'
GROUP BY CONVERT(...dbo.TB_EVENT_LOG.nDateTime...), empId
答案 2 :(得分:0)
按日期分组后,您可以获得预期的结果。对?
尝试一下。
SELECT [date],left(convert(varchar(10),DATEADD(second, utc, '19700101 00:00:00'),108),5) as [time], FunctionKey,TB_USER_CUSTOMINFO.sFieldValue8 as EMPLOYEEID
FROM (
SELECT MMDDYYYY AS [date], nUserID, MIN(nDateTime) as utc , 'F1' AS FunctionKey
FROM (
SELECT convert(varchar(10),DATEADD(second, dbo.TB_EVENT_LOG.nDateTime, '19700101 00:00:00'),103) AS MMDDYYYY
, nUserID
, nDateTime
FROM dbo.TB_EVENT_LOG
WHERE nDateTime > datediff(second, '19700101 00:00:00', DATEADD(DAY, -4, getdate()))
and nDateTime < datediff(second, '19700101 00:00:00', getdate())
and dbo.TB_EVENT_LOG.[nReaderIdn] in(539577044) ) DATE_ADDED
GROUP BY MMDDYYYY, nUserID
UNION ALL
SELECT MMDDYYYY AS [date], nUserID, MAX(nDateTime) as utc , 'F2' AS FunctionKey
FROM (
SELECT convert(varchar(10),DATEADD(second, dbo.TB_EVENT_LOG.nDateTime, '19700101 00:00:00'),103) AS MMDDYYYY
, nUserID
, nDateTime
FROM dbo.TB_EVENT_LOG
WHERE nDateTime > datediff(second, '19700101 00:00:00', DATEADD(DAY, -4, getdate()))
and nDateTime < datediff(second, '19700101 00:00:00', getdate())
and dbo.TB_EVENT_LOG.[nReaderIdn] in(539577051) ) DATE_ADDED
GROUP BY MMDDYYYY, nUserID ) TA_DATA
INNER JOIN dbo.TB_USER ON TA_DATA.nUserID = dbo.TB_USER.sUserID
INNER JOIN dbo.TB_USER_CUSTOMINFO ON dbo.TB_USER.nUserIdn = dbo.TB_USER_CUSTOMINFO.nUserIdn