Sql 时间10:45将在13:15结束。你需要清楚地定义你的规则。您应该显示“预期结果”。我在数据中没有看到“从内部打印”。“跳出”是什么意思?。一天中最早的记录怎么可能不是开始?这些是你应该在问题中解释的事情。 CREATE TABLE dbo.Use
Sql 时间10:45将在13:15结束。你需要清楚地定义你的规则。您应该显示“预期结果”。我在数据中没有看到“从内部打印”。“跳出”是什么意思?。一天中最早的记录怎么可能不是开始?这些是你应该在问题中解释的事情。 CREATE TABLE dbo.Use,sql,sql-server,Sql,Sql Server,时间10:45将在13:15结束。你需要清楚地定义你的规则。您应该显示“预期结果”。我在数据中没有看到“从内部打印”。“跳出”是什么意思?。一天中最早的记录怎么可能不是开始?这些是你应该在问题中解释的事情。 CREATE TABLE dbo.UserActivity ( [User] int NOT NULL, Mode varchar(6) NOT NULL, Activity varchar(6) NOT NULL, ActivityTime datetime NOT NUL
时间10:45将在13:15结束。你需要清楚地定义你的规则。您应该显示“预期结果”。我在数据中没有看到“从内部打印”。“跳出”是什么意思?。一天中最早的记录怎么可能不是开始?这些是你应该在问题中解释的事情。
CREATE TABLE dbo.UserActivity (
[User] int NOT NULL,
Mode varchar(6) NOT NULL,
Activity varchar(6) NOT NULL,
ActivityTime datetime NOT NULL
)
GO
--Insert data into the UserActivity table
INSERT dbo.UserActivity ([User] ,Mode , Activity, ActivityTime)
VALUES (1,'F1' ,'Divout', CAST('2013-01-01 08:30' AS datetime))
, (1,'F3' ,'Divin', CAST('2013-01-01 10:45' AS datetime))
, (1,'F3' ,'Divout', CAST('2013-01-01 13:15' AS datetime))
, (1,'F5' ,'Divin', CAST('2013-01-01 15:30' AS datetime))
, (1,'F3' ,'Divin', CAST('2013-01-01 16:15' AS datetime))
, (1,'F3' ,'Divout', CAST('2013-01-01 17:00' AS datetime))
, (1,'F1' ,'Divout', CAST('2013-01-02 08:30' AS datetime))
, (1,'F3' ,'Divin', CAST('2013-01-02 10:45' AS datetime))
, (1,'F4' ,'Divout', CAST('2013-01-02 13:00' AS datetime))
, (1,'F1' ,'Divin', CAST('2013-01-02 16:45' AS datetime))
, (2,'F1' ,'Divout', CAST('2013-01-01 8:25' AS datetime))
, (2,'F3' ,'Divin', CAST('2013-01-01 11:30' AS datetime))
, (2,'F3' ,'Divout', CAST('2013-01-01 12:35' AS datetime))
, (2,'F1' ,'Divin', CAST('2013-01-01 14:45' AS datetime))
select *,Error_Column=CASE
WHEN ((Lag(T2.Activity, 1)OVER(ORDER BY T2.[UserName],T2.EndActivityTime,T2.CurrentMode)=T2.Activity)--2 finger prints from same device
or ((T2.PreviousMode<>T2.CurrentMode) and (T2.PreviousMode is not null or T2.CurrentMode is not Null)))--inside mode differenet than outside mode
then 1
else 0
end
from (
SELECT Activity --Device Place
,CurrentMode=Mode --Device Mode
, PreviousMode = --Inter F
CASE
WHEN Activity='Divout' --UserName was out and he return
THEN Lag([Mode], 1) OVER(ORDER BY [User],ActivityTime,Mode) --give me his last leave state
Else Null --Else UserName using inside so we don't need his last leave state
End
, ActivityDate = CAST(ActivityTime As DATE) --Date of Activity
, UserName = [User] --UserName
, EndActivityTime = ActivityTime
, StartActivityTime =
CASE
WHEN Activity='Divout' --UserName was out and he return
THEN Lag(ActivityTime, 1) OVER(ORDER BY [User], ActivityTime,Mode)
Else Null
End
, EndActivityDate = CAST(ActivityTime AS DATE)
, StartActivityDate =
CASE
WHEN Activity='Divout' --UserName was out and he return
Then CAST(Lag(ActivityTime, 1) OVER(ORDER BY [User], ActivityTime ,Mode) AS DATE)
Else Null
End
FROM dbo.UserActivity
where mode<>'F1'
Union
SELECT Activity --Device Place
,CurrentMode=Mode --Device Mode
, PreviousMode = --Inter F
CASE
WHEN Activity='Divin' --End of the Day
THEN Lag([Mode], 1) OVER(ORDER BY [User],ActivityTime,Mode) --give me his Attendance Mode
Else Null --Else UserName using outside so we don't need his last leave state
End
, ActivityDate = CAST(ActivityTime As DATE) --Date of Activity
, UserName = [User] --UserName
, EndActivityTime = ActivityTime
, StartActivityTime =
CASE
WHEN Activity='Divin'
THEN Lag(ActivityTime, 1) OVER(ORDER BY [User], ActivityTime,Mode)
Else Null
End
, EndActivityDate = CAST(ActivityTime AS DATE)
, StartActivityDate =
CASE
WHEN Activity='Divin'
Then CAST(Lag(ActivityTime, 1) OVER(ORDER BY [User], ActivityTime,Mode) AS DATE)
Else Null
End
FROM (select * from dbo.UserActivity where mode='F1') T1)T2
ORDER BY T2.[UserName],T2.EndActivityTime,T2.CurrentMode
select
*
from (
select
CAST(ActivityTime AS date) act_date
, row_number() over(partition by [User], CAST(ActivityTime AS date)
order by ActivityTime ASC) start_row
, row_number() over(partition by [User], CAST(ActivityTime AS date)
order by ActivityTime DESC) end_row
, [User]
, Mode
, Activity
, ActivityTime
from UserActivity
) d
order by act_date, [User], start_row
;
act_date | start_row | end_row | User | Mode | Activity | ActivityTime
:----------|-----------|-------- | -----|------|----------|--------------------
01/01/2013 | 1 | 6 | 1 | F1 | Divout | 01/01/2013 08:30:00
01/01/2013 | 2 | 5 | 1 | F3 | Divin | 01/01/2013 10:45:00
01/01/2013 | 3 | 4 | 1 | F3 | Divout | 01/01/2013 13:15:00
01/01/2013 | 4 | 3 | 1 | F5 | Divin | 01/01/2013 15:30:00
01/01/2013 | 5 | 2 | 1 | F3 | Divin | 01/01/2013 16:15:00
01/01/2013 | 6 | 1 | 1 | F3 | Divout | 01/01/2013 17:00:00
01/01/2013 | 1 | 4 | 2 | F1 | Divout | 01/01/2013 08:25:00
01/01/2013 | 2 | 3 | 2 | F3 | Divin | 01/01/2013 11:30:00
01/01/2013 | 3 | 2 | 2 | F3 | Divout | 01/01/2013 12:35:00
01/01/2013 | 4 | 1 | 2 | F1 | Divin | 01/01/2013 14:45:00
02/01/2013 | 1 | 4 | 1 | F1 | Divout | 02/01/2013 08:30:00
02/01/2013 | 2 | 3 | 1 | F3 | Divin | 02/01/2013 10:45:00
02/01/2013 | 3 | 2 | 1 | F4 | Divout | 02/01/2013 13:00:00
02/01/2013 | 4 | 1 | 1 | F1 | Divin | 02/01/2013 16:45:00