这个SQL查询有什么问题?

时间:2010-12-18 12:59:47

标签: sql

SELECT [travel], [fro_m], [t_o], [dep], [arr], [fare], [discount], [faresleeper],
[rating], [seats], [s_no],
[booking_closed] = 
   CASE WHEN s1from <= @date AND s1to >= @date THEN s1Rate ELSE fare END 
        WHEN s2from <= @date AND s2to >= @date THEN s2Rate ELSE fare END 
        WHEN s3rate <= @date AND s3to >= @date THEN s3Rate ELSE fare END 
FROM a1_volvo WHERE (fro_m = @fro_m) AND (t_o = @t_o)

4 个答案:

答案 0 :(得分:2)

案例陈述不正确:

CASE WHEN s1from <= @date AND s1to >= @date THEN s1Rate 
WHEN s2from <= @date AND s2to >= @date THEN s2Rate 
WHEN s3rate <= @date AND s3to >= @date THEN s3Rate 
else fare END

在case语句中的每一行之后你有else语句。它应该只在最后。

MSDN Case Statement

答案 1 :(得分:1)

在第三个WHEN选项中,您要将名称为s3rate的字段与日期进行比较。我不知道这些字段是什么,但在我看来,这不是日期字段...

另请查看Kevin&amp; BobTodd回答另一个问题...

答案 2 :(得分:0)

CASE WHEN s1from <= @date AND s1to >= @date THEN s1Rate 
        WHEN s2from <= @date AND s2to >= @date THEN s2Rate 
        WHEN s3rate <= @date AND s3to >= @date THEN s3Rate 
    ELSE fare 
END 

更好吗?

答案 3 :(得分:0)

这与他的代码相同,但在和之间使用。它们使用它的优点是,它更好的可读性。在进行代码审查时,您需要花费无穷无尽的时间来比较两个名称是否类型相同

 CASE WHEN @date between s1from AND s1to THEN s1Rate 
      WHEN @date between s2from AND s2to THEN s2Rate 
      WHEN @date between s3rate AND s3to THEN s3Rate 
      ELSE fare 
END