选择不会工作

时间:2013-06-18 10:49:34

标签: sql syntax syntax-error

这个查询的狄更斯出了什么问题?

SELECT id,
       SUM(CASE myDate
             WHEN DATEDIFF(day, myDate, GETDATE()) = 0 THEN [Items]
             ELSE 0
           END)
FROM   myTable
GROUP  BY id 

???

错误说

"Incorrect syntax near '='.

4 个答案:

答案 0 :(得分:0)

SUM(CASE myDate WHEN DATEDIFF(day, myDate, GETDATE()) )你错过了一个封闭的支架我想

答案 1 :(得分:0)

这是您想要的查询:

SELECT id,
       SUM(CASE WHEN DATEDIFF(day, myDate, GETDATE()) = 0 THEN [Items] ELSE 0 END)
FROM myTable
GROUP BY id;

myDate之后的case没有为您做任何事情。语法上允许case语句的替代形式:

sum(case mydate when '2013-01-01' then 'New Years Day' . . .

但是当你在when之后有一个表达时。

答案 2 :(得分:0)

mydate

之后您不需要CASE
SELECT  id, 
        SUM(CASE 
            WHEN DATEDIFF(day, myDate, GETDATE()) = 0 
            THEN [Items] ELSE 0 END) 

FROM myTable GROUP BY id;

答案 3 :(得分:0)

SELECT id,        SUM(CASE myDate              当DATEDIFF(day,myDate,GETDATE()= 0)那么[Items]              ELSE 0            结束) 来自myTable GROUP BY id

这是正确的