根据sql server中的条件获取Flag值

时间:2018-04-09 16:04:53

标签: sql-server sql-server-2012

我对SQL Server有疑问。  请告诉我如何根据条件在SQL Server中使用时间计算标记值。 如果超出时间差超过5小时,则标志为1,否则为0

Table : employee
CREATE TABLE [dbo].[employee](
    [Emp Id] [float] NULL,
    [Area Of Access] [nvarchar](255) NULL,
    [Time] [datetime] NULL,
    [floor] [nvarchar](255) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (101, N'IN', CAST(N'2018-03-03T03:36:58.000' AS DateTime), N'Common Area')
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (101, N'OUT', CAST(N'2018-03-03T03:38:55.000' AS DateTime), N'Common Area')
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (101, N'OUT', CAST(N'2018-03-05T18:54:00.000' AS DateTime), N'Common Area')
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (101, N'IN', CAST(N'2018-03-05T18:54:13.000' AS DateTime), N'Common Area')
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (101, N'IN', CAST(N'2018-03-05T18:54:32.000' AS DateTime), N'Common Area')
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (101, N'IN', CAST(N'2018-03-05T18:55:01.000' AS DateTime), N'Production Floor')
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (101, N'OUT', CAST(N'2018-03-05T19:58:41.000' AS DateTime), N'Production Floor')
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (100, N'IN', CAST(N'2018-02-06T16:03:08.000' AS DateTime), N'Production Floor')
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (100, N'OUT', CAST(N'2018-02-06T22:01:40.000' AS DateTime), N'Production Floor')
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (100, N'IN', CAST(N'2018-02-06T22:42:15.000' AS DateTime), N'Production Floor')
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (100, N'OUT', CAST(N'2018-02-07T00:33:19.000' AS DateTime), N'Production Floor')
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (100, N'IN', CAST(N'2018-02-07T10:13:11.000' AS DateTime), N'Production Floor')
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (100, N'OUT', CAST(N'2018-02-07T15:13:10.000' AS DateTime), N'Production Floor')
GO

根据上表我想要输出如下:

EmpId   |Area Of Access |Time                           |floor                  |Flag
100 |IN         |2018-02-06 16:03:08.000    |Production Floor   |0
100 |OUT            |2018-02-06 22:01:40.000    |Production Floor   |0
100 |IN         |2018-02-06 22:42:15.000    |Production Floor   |0
100 |OUT            |2018-02-07 00:33:19.000    |Production Floor   |0
100 |IN         |2018-02-07 10:13:11.000    |Production Floor   |1
100 |OUT            |2018-02-07 15:13:10.000    |Production Floor   |0
101 |IN          |2018-03-03 03:36:58.000   |Common Area            |0
101 |OUT             |2018-03-03 03:38:55.000   |Common Area            |0
101 |OUT             |2018-03-05 18:54:00.000   |Common Area            |1
101 |IN          |2018-03-05 18:54:13.000   |Common Area            |0
101 |IN          |2018-03-05 18:54:32.000   |Common Area            |0
101 |IN          |2018-03-05 18:55:01.000   |Production Floor   |0
101 |OUT             |2018-03-05 19:58:41.000   |Production Floor   |0

我试过如下:

select el.*,  
case when (datediff(hh, lag(Time) over (partition by [Emp Id] order by Time), Time)) > 5
and el.[Area Of Access]='in' then 1  
when (datediff(hh, lag(Time) over (partition by [Emp Id]  order by Time), Time)) > 5
then 1   else 0 end  Flag from employee el 

以上查询未给出预期结果。请告诉我如何在SQL Server

中实现此任务

1 个答案:

答案 0 :(得分:0)

你可以使用滞后函数,如下所示:

SELECT el.*,  
    CASE WHEN ((DATEDIFF(hh, LAG(Time) OVER (PARTITION BY [Emp Id] ORDER BY Time), Time)) > 5 
            AND LAG([Area Of Access]) OVER (PARTITION BY [Emp Id] ORDER BY Time) = 'out')
        THEN 1  
        ELSE 0 
    END  Flag 
FROM employee el