使用MIN-算术溢出时,将表达式转换为数据类型datetime时出错



下面的查询抛出了一个错误,我不太明白为什么:

      Select * from Correspondences
      where case_id in(000021,00000991,000081,0000731)
      and case_id in(Select min(comm_date_utc) from Correspondences where case_id in               (000021,00000991,000081,0000731))

SQL server出现以下错误:Msg 8115,级别16,状态2,第1行将表达式转换为数据类型datetime时发生算术溢出错误。

有人能帮助我理解为什么以及我应该如何绕过这一点吗?

我想这就是你想要的:

  Select c.*
  from Correspondences c
  where case_id in(000021,00000991,000081,0000731)
  and comm_date_utc = (Select min(comm_date_utc)
                       from Correspondences c2
                       where c2.case_id = c.case_id
                      );

查询的问题在于它将caseid与日期时间进行比较。此查询根据时间返回每个案例的第一条记录。后者似乎更可能是查询的意图。

最新更新