获取错误的总产量?

时间:2009-10-06 06:51:36

标签: sql-server

实施例

我想计算09:00:00到18:00:00之间的工作时间,

09:00:00 - Normal_Intime
18:00:00 - Normal_Outtime

假设

1) Intime is 10:00:00, Outtime is 17:00:00 It should give the output as 07:00:00
2) Intime is 08:00:00. Outtime is 19:00:00 It should give the output as 09:00:00

以上输出应与Normal_Intime,Normal_Outtime进行比较。

意味着Normal_Intime仅为09:00:00而Normal_Output仅为18:00:00,因此Intime不应该> 09:00:00和Outtime< 18:00:00。

查询

select Intime, Outtime, CONVERT(char(8), CASE WHEN OutTime > Normal_Outtime THEN cast(Normal_Outtime AS datetime) ELSE cast(OutTime AS datetime) END - CASE WHEN InTime > Normal_Intime THEN cast(InTime AS datetime) ELSE cast(Normal_Intime AS datetime) END, 8) AS WorkTime from table

输出

Intime       Outtime        Worktime
15:37:48    22:08:15    02:22:12
07:33:34    07:59:13    22:59:13

第二行输出错误它应该只给出00:25:39

如果输入和输出时间都小于NormalInTime,并且输入和输出时间都大于NormalOutTime。如何为此

制定条件

如何查询此条件。

需要查询帮助。

3 个答案:

答案 0 :(得分:2)

这可以让你得到你想要的东西

DECLARE @Table TABLE(
        InTime DATETIME,
        OutTime DATETIME
)

INSERT INTO @Table (InTime,OutTime) SELECT '15:37:48', '22:08:15'
INSERT INTO @Table (InTime,OutTime) SELECT '07:33:34', '07:59:13'

DECLARE @Normal_InTime DATETIME,
        @Normal_OutTime DATETIME

SELECT  @Normal_InTime = '09:00:00',
        @Normal_OutTime = '18:00:00'

SELECT  *,
        --both dates are smaller than NotmalInTime
        CASE
            WHEN InTime < @Normal_InTime AND OutTime < @Normal_InTime THEN OutTime - InTime
            WHEN OutTime > @Normal_OutTime AND InTime > @Normal_OutTime THEN OutTime - InTime
            WHEN OutTime > @Normal_OutTime 
                THEN @Normal_OutTime - InTime
            WHEN InTime < @Normal_InTime
                THEN OutTime - @Normal_InTime
            ELSE OutTime - InTime
        END
FROM    @Table

但你需要考虑到。当用户在24:00之前预订然后在第二天预订时会发生什么。

正如我所说,如果预订和预订在NormalTimeIn之前或在NormalTimeOut之后会发生什么?

你需要指定它。

答案 1 :(得分:1)

在计算时间的同时你也应该考虑这个日期。

declare 
@Normal_Intime datetime,
@Normal_Outtime datetime,
@Actual_Intime datetime,
@Actual_Outtime datetime
set @Normal_Intime = cast('09:00:00' as datetime)
set @Normal_Outtime =cast('18:00:00' as datetime) 

-- CASE 1
set @Actual_Outtime = cast('17:00:00' as datetime)  
set @Actual_Intime = cast('10:00:00' as datetime)  

select case when @Actual_Intime > @Normal_Intime then @Actual_Intime else @Normal_Intime end -
case when @Actual_Outtime < @Normal_Outtime then @Actual_Outtime else @Normal_Outtime end

-- CASE 2    

set @Actual_Outtime = cast('19:00:00' as datetime)    
set @Actual_Intime = cast('08:00:00' as datetime)    

select case when @Actual_Intime > @Normal_Intime then @Actual_Intime else @Normal_Intime end -
case when @Actual_Outtime < @Normal_Outtime then @Actual_Outtime else @Normal_Outtime end

-- CASE 3  

set @Actual_Outtime = cast('07:59:13' as datetime)    
set @Actual_Intime = cast('07:33:34' as datetime)    

select case when @Actual_Intime > @Normal_Intime then @Actual_Intime else @Normal_Intime end -
case when @Actual_Outtime < @Normal_Outtime then @Actual_Outtime else @Normal_Outtime end

答案 2 :(得分:0)

你试过这个吗:

SELECT CONVERT(varchar, Outtime - Intime, 108) FROM YourTable

Outtime - Intime会为您提供所需的时间跨度,并使用108参数进行转换,只会在该时间段内为您提供小时,分钟和秒数more info about convert