我无法避免此查询中的重复记录:
select
u.employeeid [id_empleado], a.LOGDATETIME [checada], ASSETID [dispositivo]
from
[dbo].[AccessLog] a
inner join
[dbo].[BiometricDevice] b on (b.ID = a.MORPHOACCESSID)
inner join
[dbo].[User_] u on (a.userid = u.id)
order by
id_empleado, checada asc
输出示例:
id_empleado checada dispositivo
14907 2016-08-22 13:25:40.000 02
14907 2016-08-22 23:32:10.000 01
14907 2016-08-23 12:08:03.000 01
14907 2016-08-23 22:51:39.000 02
14907 2016-08-24 12:29:07.000 01
14907 2016-08-26 23:24:55.000 01
14907 2016-08-30 00:29:08.000 01
16034 2016-10-05 12:51:51.000 GERENCIA
16034 2016-10-06 04:03:56.000 GERENCIA
16034 2016-10-08 11:37:29.000 GERENCIA
16034 2016-10-09 10:52:51.000 GERENCIA
16034 2016-10-10 00:51:16.000 GERENCIA
16034 2016-10-10 11:19:42.000 GERENCIA
我需要什么:
id_empleado checada dispositivo
14907 2016-08-22 13:25:40.000 02
16034 2016-10-10 11:19:42.000 GERENCIA
答案 0 :(得分:0)
请尝试以下方法。它会选择与给定checada
相关联的第一个dispositivo
和id_empleado
。
select [id_empleado], [checada], [dispositivo]
from (
select u.employeeid [id_empleado],a.LOGDATETIME [checada], ASSETID [dispositivo],
ROW_NUMBER() OVER(PARTITION BY u.employeeid ORDER BY a.LOGDATETIME) as seq
from [dbo].[AccessLog] a
inner join [dbo].[BiometricDevice] b on (b.ID = a.MORPHOACCESSID)
inner join [dbo].[User_] u on (a.userid = u.id)) as a
where seq = 1
order by id_empleado, checada asc
输出:
id_empleado checada dispositivo
----------- ----------------------- ------------------------------
14907 2016-08-22 13:25:40.000 02
16034 2016-10-05 12:51:51.000 GERENCIA
参考: MSDN ROW_NUMBER (Transact-SQL)
答案 1 :(得分:0)
这应该有效:
select t.*
from
(select
u.employeeid, a.logdatetime, assetid,
row_number() over (partition by u.employeeid order by u.employeeid) as r
from
[dbo].[AccessLog] a
inner join
[dbo].[BiometricDevice] b on (b.ID = a.MORPHOACCESSID)
inner join
[dbo].[User_] u on (a.userid = u.id)
) t
where r = 1
答案 2 :(得分:0)
使用上面帖子中提到的 GROUP BY 或 ROW_NUMBER()
select
u.employeeid [id_empleado], MAX(a.LOGDATETIME) [checada], MAX(ASSETID) [dispositivo]
from
[dbo].[AccessLog] a
inner join
[dbo].[BiometricDevice] b on (b.ID = a.MORPHOACCESSID)
inner join
[dbo].[User_] u on (a.userid = u.id)
GROUP BY u.employeeid
order by
id_empleado, checada asc