我想通过fromDate& amp;关于员工代码的日期

时间:2017-06-06 07:03:30

标签: sql stored-procedures sql-server-2012

这是我的存储过程...这个存储过程显示了员工代码的fromdate和toate的结果,用于区分checkIn和checkout的聚合函数... 它只显示一个日期的结账时间数据,问题是,我需要多天这个......

如果有人知道什么是问题,请与我分享您的经验,并在查询中指出我的错误... 我会感恩的

这是表脚本

 CREATE TABLE [dbo].[ras_AttRecord](
        [ID] [int] IDENTITY(1,1) NOT NULL,
        [DN] [smallint] NULL,
        [DIN] [bigint] NOT NULL,
        [Clock] [datetime] NOT NULL,
        [VerifyMode] [tinyint] NULL,
        [AttTypeId] [char](3) NOT NULL,
        [CollectDate] [datetime] NOT NULL,
        [LastUpdatedUID] [int] NULL,
        [LastUpdatedDate] [datetime] NULL,
        [Remark] [nvarchar](64) NULL
        )

    CREATE TABLE [dbo].[ras_Users](
        [UID] [int] NOT NULL,
        [DIN] [bigint] NOT NULL,
        [PIN] [varchar](32) NOT NULL,
        [UserName] [nvarchar](64) NULL,
        [Sex] [char](1) NOT NULL,
        [Password] [nvarchar](32) NULL,
        [PasswordQuestion] [nvarchar](64) NULL,
        [PasswordAnswer] [nvarchar](32) NULL,
        [IsApproved] [bit] NOT NULL,
        [IsLockedOut] [bit] NOT NULL,
        [CreateDate] [datetime] NULL,
        [LastLoginDate] [datetime] NULL,
        [DeptId] [varchar](64) NOT NULL,
        [AttId] [char](4) NULL,
        [RuleId] [char](4) NULL,
        [WeekendId] [char](4) NULL,
        [LastUpdatedUID] [int] NULL,
        [LastUpdatedDate] [datetime] NOT NULL,
        [Comment] [nvarchar](128) NULL
        )

我正在使用此存储过程

ALTER  PROCEDURE [dbo].[GetLateComersmonthly]       
    @FromDate DATE='05/22/2017', 
    @ToDate DATE='06/06/2017',       
    @Code varchar(6)  ='3155'      
AS       
BEGIN        
    -- SET NOCOUNT ON added to prevent extra result sets from        
    -- interfering with SELECT statements.        
    SET NOCOUNT ON;       
    IF  @Code = '0'        
    SET @Code = null      

    -- Insert statements for procedure here        
    select 
        T1.UserName, 
        T1.DIN as [DIN], 
        min(T1.Date) as [Date], 
        min(T1.Time) as [Time], 
        CASE   
            WHEN max(T2.Time) = max(T1.Time) THEN '' 
            ELSE max(T2.Time)  end as [Timee], min(T1.Day) as [Day],
        CASE   
            WHEN concat(CONVERT(varchar,DATEDIFF(MINUTE,min(T1.Time),max(t2.Time)),108)/60,
                         ':', 
                        CONVERT(varchar,DATEDIFF(MINUTE,min(T1.Time),max(t2.Time))%60,108)) = '0:0' 
            THEN 'No Check Out' 
            ELSE concat(CONVERT(varchar,DATEDIFF(MINUTE,min(T1.Time),max(t2.Time)),108)/60, 
                        ':',
                        CONVERT(varchar,DATEDIFF(MINUTE,min(T1.Time),max(t2.Time))%60,108)) 
            END AS hrs
    from 
        (SELECT 
            UserName , 
            ru.DIN, 
            CONVERT(varchar, Att.Clock, 101) AS Date, 
            CONVERT(varchar, Att.Clock, 108) AS Time, 
            att.VerifyMode as Mode, DATENAME(dw, Att.Clock) AS Day        
        FROM 
            ras_Users as ru        
            inner join ras_AttRecord as att ON ru.DIN = att.Din        
        where  CONVERT(DATE,Clock) >= @FromDate 
                and CONVERT(DATE,Clock)<=@ToDate 
                and att.DIN = COALESCE (@Code,  att.DIN) 
                and att.VerifyMode = 15   
        ) as T1 
        left join 
        (SELECT  
            ru.DIN, 
            max(CONVERT(varchar, Att.Clock, 108)) AS Time  
         FROM 
            ras_Users as ru        
            inner join ras_AttRecord as att ON ru.DIN = att.Din        
         where  CONVERT(DATE,Clock) >= @FromDate 
                and  CONVERT(DATE,Clock)<=@ToDate 
                and att.DIN = COALESCE (@Code,  att.din) 
         group by ru.DIN, att.Clock 
        ) as  T2  on T1.DIN = T2.DIN --and DATEDIFF(MINUTE,T1.Time,t2.Time)/60 < 9
    group by 
        T1.Username, 
        T1.din,
        T1.Day      
    -- having CONVERT(varchar, min(T1.Time), 108) > '09:30:00'   
    order by T1.din

END 

显示以下结果

当前记录显示

Userid  Date            CheckIn         CheckOut    Day       total hours
    3155    05/26/2017  09:15:10    17:15:00  Friday      8:0
    3155    05/29/2017  09:08:36    17:15:00  Monday      8:7
    3155    05/25/2017  09:34:28    17:15:00  Thursday        7:41
    3155    05/30/2017  09:15:12    17:15:00  Tuesday     8:0
    3155    05/31/2017  09:27:37    17:15:00  Wednesday       7:48

我需要以下结果 预期结果

  Userid      Date        CheckIn         CheckOut         day  total hours
3155    05/26/2017  09:15:10    17:00:00    Friday    8:0
3155    05/29/2017  09:08:36    17:05:00    Monday    8:7
3155    05/25/2017  09:34:28    17:30:00    Thursday  7:41
3155    05/30/2017  09:15:12    17:18:00    Tuesday   8:0
3155    05/31/2017  09:27:37    17:10:00    Wednesday 7:48

0 个答案:

没有答案