emp logindetails基于sql server中的条件

时间:2018-04-06 14:08:56

标签: sql-server sql-server-2008 sql-server-2012

我对SQL Server有疑问。请告诉我如何根据条件解决SQL Server中的登录和注销时间计算。

  1. 如果相同的empid工作多班,同一日期的多班次计算必须是时差为5小时,那么该日期视为该emp的多班次
  2. OnFloor时间花了多少时间
  3. OffFloor时间花了多少时间
  4. 示例输入数据:

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

    根据以上数据,我想要输出如下:

    enter image description here

    我试过如下:

    select
        ShiftDate, ShitStartTime, ShiftEndTime
        , Total_Time = right(concat('0', Total_Time / 3600), 2) + ':' + right(concat('0', Total_Time % 3600 / 60), 2) + ':' + right(concat('0', Total_Time % 60), 2)
        , OnFloor = right(concat('0', OnFloor / 3600), 2) + ':' + right(concat('0', OnFloor % 3600 / 60), 2) + ':' + right(concat('0', OnFloor % 60), 2)
        , OffFloor = right(concat('0', OffFloor / 3600), 2) + ':' + right(concat('0', OffFloor % 3600 / 60), 2) + ':' + right(concat('0', OffFloor % 60), 2)
        , [Emp ID]
    from (
        select
            [Emp ID], ShiftDate = cast(min(Time) as date)
            , ShitStartTime = isnull( min( case when ltrim(rtrim([Area Of Access]))='in'
            then 
            Time end  ) ,'1900-01-01')
            , ShiftEndTime = isnull( max( case when ltrim(rtrim([Area Of Access]))='out'
            then 
            Time end  ) ,'1900-01-01')
            , Total_Time = sum(ss)
            , OnFloor = sum(iif(ltrim(rtrim([Area Of Access]))='in', ss, 0))
    
            , OffFloor = sum(iif(ltrim(rtrim([Area Of Access]))='out', ss, 0))
    from (
            select
                *, ss = datediff(ss, Time, lead(Time) over (partition by [Emp ID], grp order by Time))
            from (
                select
                    *, grp = sum(diff) over (partition by [Emp ID] order by Time)
                from (
                    select
                        *, diff = iif(datediff(mi, lag(Time) over (partition by [Emp ID] order by Time), Time) > 300
    
                         and [Area Of Access]='in'
                         , 1, 0)
                    from
                        emp
                ) t
            ) t
        ) t
        group by [Emp ID], grp
    ) t
    

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

    中实现此任务

0 个答案:

没有答案