简体   繁体   English

日期之间的总和,T-SQL错误

[英]Sum of Dates between, T-SQL error

I'm trying to check if one of the join_date or date_of_change (date fields) are within the range and count them but I get an error: 我正在尝试检查join_date或date_of_change(日期字段)之一是否在范围内,并对它们进行计数,但出现错误:

sqlserver.jdbc.SQLServerException: The conversion from int to TIMESTAMP is unsupported. sqlserver.jdbc.SQLServerException:不支持从int到TIMESTAMP的转换。

SUM(CASE WHEN (join_date BETWEEN DATEADD(day, -8, GETDATE()) AND DATEADD(day, -1, GETDATE())) OR (date_of_change BETWEEN DATEADD(day, -8, GETDATE()) AND DATEADD(day, -1, GETDATE())) THEN 1 ELSE 0 END) AS Total

Could someone explain to me what I'm doing wrong. 有人可以向我解释我在做什么错。

Original Code: 原始代码:

  SELECT DISTINCT mtype, CASE WHEN (join_date BETWEEN DATEADD(day, -8, 
GETDATE()) AND DATEADD(day, -1, GETDATE())) OR (date_of_change BETWEEN 
DATEADD(day, -8, GETDATE()) AND DATEADD(day, -1, GETDATE())) THEN 1 ELSE 0 
END AS Total FROM T0 GROUP BY mype, join_date,date_of_change

As @Alex K has said there could be another statement in the batch causing the problem as there doesn't seem to be any TimeStamp involved in this query. 正如@Alex K所说,该批处理中可能没有其他语句,因为该查询中似乎没有涉及任何时间戳。

Answering your last comment about the GROUP BY, you can simplify the query the following way: 回答关于GROUP BY的最新评论时,可以通过以下方式简化查询:

SELECT 
    mtype, COUNT(1) as Total
FROM 
    T0 
WHERE 
    (join_date BETWEEN DATEADD(day, -8, GETDATE()) AND DATEADD(day, -1, GETDATE())) 
    OR (date_of_change BETWEEN DATEADD(day, -8, GETDATE()) AND DATEADD(day, -1, GETDATE())) 
GROUP BY 
    mype

But I am afraid that if the error is in a different statement. 但是,如果错误在不同的语句中,则恐怕会出错。

我再次写了查询,它有效了,一定是某个地方有错字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM