我的记录如下
Login Time Logout Time User Shift
2017-04-01 21:30:00.000 2017-04-02 00:00:00.000 I0402 N
2017-04-02 00:00:00.000 2017-04-02 00:30:00.000 I0402 N
2017-04-02 01:30:00.000 2017-04-02 05:30:00.000 I0402 N
2017-04-02 06:30:00.000 2017-04-02 08:30:00.000 I0402 N
我希望为用户获得最早的进入时间和最新的注销时间。我怎么能这样做?
我希望记录如下
Login Time Logout Time User Shift
2017-04-01 21:30:00.000 2017-04-02 08:30:00.000 I0402 N
有可能吗? 感谢。
修改
我之前尝试使用Max和Min,并按用户和Shift进行分组,这是有效的。如果记录超过1天,那似乎不起作用
2017-04-01 21:30:00.000 2017-04-02 00:00:00.000 I0402 N
2017-04-02 00:00:00.000 2017-04-02 00:30:00.000 I0402 N
2017-04-02 01:30:00.000 2017-04-02 05:30:00.000 I0402 N
2017-04-02 06:30:00.000 2017-04-02 08:30:00.000 I0402 N
2017-04-02 21:30:00.000 2017-04-03 00:00:00.000 I0402 N
2017-04-03 00:00:00.000 2017-04-03 00:30:00.000 I0402 N
2017-04-03 01:30:00.000 2017-04-03 05:30:00.000 I0402 N
预期结果如下
2017-04-01 21:30:00.000 2017-04-02 08:30:00.000 I0402 N
2017-04-02 21:30:00.000 2017-04-03 05:30:00.000 I0402 N
答案 0 :(得分:3)
SELECT
User
,MIN(Login Time)
,MAX(Logout Time)
,Shift
FROM
dbo.table
GROUP BY
User,Shift
答案 1 :(得分:0)
我之前尝试使用Max和Min,并按用户和Shift进行分组,这是有效的。如果记录超过1天,那似乎不起作用
2017-04-01 21:30:00.000 2017-04-02 00:00:00.000 I0402 N
2017-04-02 00:00:00.000 2017-04-02 00:30:00.000 I0402 N
2017-04-02 01:30:00.000 2017-04-02 05:30:00.000 I0402 N
2017-04-02 06:30:00.000 2017-04-02 08:30:00.000 I0402 N
2017-04-02 21:30:00.000 2017-04-03 00:00:00.000 I0402 N
2017-04-03 00:00:00.000 2017-04-03 00:30:00.000 I0402 N
2017-04-03 01:30:00.000 2017-04-03 05:30:00.000 I0402 N
预期结果如下
2017-04-01 21:30:00.000 2017-04-02 08:30:00.000 I0402 N
2017-04-02 21:30:00.000 2017-04-03 05:30:00.000 I0402 N
答案 2 :(得分:0)
您可以使用以下查询来获取结果
SELECT T1.USER, T2.MAX_LOGIN_TIME, T3.MIN_LOGIN_TIME FROM
TABLE_NAME T1
INNER JOIN (SELECT MAX(LOGIN_TIME) AS MAX_LOGIN_TIME,USER FROM TABLE_NAME
GROUP BY USER) T2
ON (T1.USER = T2.USER)
INNER JOIN (SELECT MIN(LOGIN_TIME) AS MIN_LOGIN_TIME,USER FROM TABLE_NAME
GROUP BY USER) T3
ON (T1.USER = T3.USER);
答案 3 :(得分:0)
看起来你真正想要的是每天一行,用户和班次。所以按这些分组并花费最小和最长时间。
select min(Login_time) as login, max(logout_time) as logout, user, shift
from mytable
group by date(login_time), user, shift
order by date(Login_time), user, shift;
这里DATE(login_time)
只是一个例子。如何从日期时间获取日期不同于DBMS到DBMS。查找DBMS的日期/时间函数以获得适当的功能。