最小内部日期差异

时间:2016-10-25 08:03:15

标签: sql ms-access ms-access-2010

我在MS Access Table中有3个字段A,B和C,名为TBL。我试图使用日期差异功能找到以小时为单位的日期差异。我想找到最小字段A,B和字段C之间的日期差异。我尝试使用以下代码实现它

SELECT Datediff("h",Min(A,B),C) FROM TBL

上面的函数给出了一个错误,错误的查询表达式中的函数使用了错误的参数数量。我怎样才能实现上述目标

2 个答案:

答案 0 :(得分:0)

您可以使用IIF

SELECT IIF(DATEDIFF("h", A, C) < DATEDIFF("h", B, C),
           DATEDIFF("h", A, C),
           DATEDIFF("h", B, C))
FROM TBL

<强>更新

如果您想要小时的小数结果,可以尝试

DATEDIFF("m", A, C) / 60.0

答案 1 :(得分:0)

如果您希望找到绝对差异,您需要Abs:

SELECT 
    *,
    IIf(Abs(DateDiff("h", A, C)) < Abs(DateDiff("h", B, C)),
        Abs(DateDiff("h", A, C)), Abs(DateDiff("h", B, C))) As
    HourDiffMin
FROM 
   TBL

如果 - 不太清楚 - 如果还可以返回A和B之间的差异,请展开IIf表达式来覆盖这个 - 非常uggly:

SELECT 
    *,
    IIf(Abs(DateDiff("h", A, B)) <
        IIf(Abs(DateDiff("h", A, C)) < Abs(DateDiff("h", B, C)),
            Abs(DateDiff("h", A, C)), Abs(DateDiff("h", B, C))),
        Abs(DateDiff("h", A, B)),
        IIf(Abs(DateDiff("h", A, C)) < Abs(DateDiff("h", B, C)),
            Abs(DateDiff("h", A, C)), Abs(DateDiff("h", B, C)))) As
    HourDiffMin
FROM 
   TBL