DateDiff中的CASE语句(Amazon Redshift)

时间:2017-03-24 15:28:21

标签: sql amazon-redshift

我正在尝试在datediff函数中实现CASE语句,但它会抛出 import java.util.Scanner; public class Driver { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("Please enter 3 digits for your code."); int a = keyboard.nextInt(); int b = keyboard.nextInt(); int c = keyboard.nextInt(); CombinationLock cl = new CombinationLock(a,b,c); System.out.println(cl.toString()); } }

即使使用如下的简化查询,也会出现错误:

public class CombinationLock { private int a; private int b; private int c; public CombinationLock(int a,int b,int c) { this.a=a; this.b=b; this.c=c; } public String toString() { String formatcode = "Clockwise " + a + "- Counter-Clockwise " + b + "- Clockwise " + c; return formatcode; } }

是否无法在函数中包含CASE语句?

1 个答案:

答案 0 :(得分:2)

Redshift is a keyworddatediff()的第一个参数。所以,你不能简单地做你想做的事。

相反:

select (case when 1=1
             then datediff(month, '2009-01-01', '2009-12-31')
             else datediff(month, '2009-01-01', '2009-12-31')
        end) as nummonths;

我认为这是过于简化的代码,因为显然在此查询的任何版本中都不需要case