0

这个查询到底有什么问题?

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

4 回答 4

1

这是您想要的查询:

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.

于 2013-06-18T10:53:12.397 回答
0

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

这是对的

于 2013-06-18T12:00:19.813 回答
0

你不需要mydate之后CASE

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

FROM myTable GROUP BY id;
于 2013-06-18T10:53:27.850 回答
0

SUM(CASE myDate WHEN DATEDIFF(day, myDate, GETDATE()) )我猜你错过了一个封闭的括号

于 2013-06-18T10:51:30.583 回答