【发布时间】:2018-01-12 06:05:12
【问题描述】:
SELECT
Duration = Case
When ([Start_Datetime] <= [MIN_START]) and [End_Datetime] Between [MIN_START] and [MAX_END] Then [MIN_START] - [End_Datetime]
When ([Start_Datetime] <= [MIN_START]) and ([MIN_START] and [MAX_END] ) Between ([Start_Datetime] and [End_Datetime]) Then [MAX_END] - [MIN_START]
When ([Start_Datetime] >= [MAX_END]) and [MAX_END] Between [Start_Datetime] and [End_Datetime] Then [MAX_END] - [Start_Datetime]
When ([Start_Datetime] >= [MAX_END]) and ([Start_Datetime] and [End_Datetime] ) Between ([MIN_START] and [MAX_END] ) Then [End_Datetime] - [Start_Datetime]
Else 0
From DB1
Example [Start_Datetime] [EndTime] [MIN_START][MAX_END] [Result]
1 1 6 2 7 4
2 2 6 3 5 2
3 4 7 3 6 2
4 3 4 2 7 1
我尝试使用 And and Between My SQL 错误 它说
在上下文中指定的非布尔类型的表达式 条件是预期的,在“和”附近。
我只是想知道如何纠正这个谢谢
【问题讨论】:
-
([Start_Datetime] <= [MIN_START]) and ([MIN_START] and [MAX_END] ) Between ([Start_Datetime] and [End_Datetime]) Then [MAX_END] - [MIN_START]这是无效的。你想达到什么目的 -
([MIN_START] and [MAX_END] ) Between没有意义。您一次只能检查一个字段是否介于其他字段之间。您可能需要两个 BETWEEN 语句,一个用于这两个日期中的每一个,它们之间有一个 AND。 -
不相关 - 你缺少一个 END。
-
我正在尝试计算 2 时间范围 EX:开始时间 4 -10 之间的重叠持续时间。结束时间 3-5。重叠 1 小时
-
BETWEEN不应与括号一起使用
标签: sql sql-server