这个简单的 SQL Server 2008 R2 查询有什么问题?产生“非布尔类型的表达式...”错误
我试图从表 Responses 中选择满足几个条件并且在 update_datetime 字段中具有最新日期的单行。 (我实际上想要 Responses 行中的所有字段,但我只会满足其 response_uuid。)
我在下面的查询中的尝试产生了错误“在条件为的上下文中指定的非布尔类型的表达式”预期,接近')'”。我到底做错了什么?
select response_uuid, MAX(update_datetime)
from Responses
where question_id=2115
and session_uuid in (
select session_uuid from Sessions
where client_uuid = '552782A2-4DC6-4715-B278-4C7F5F867975'
)
group by response_uuid
having MAX(update_datetime)
我在互联网上关注过类似的问题,但我只是不明白问题是什么。感谢您的帮助!
ps 如果您有兴趣查看整个查询,或者查看全部查询会有所帮助,那么这实际上只是一个更大查询的一小部分。
I am trying to choose the single row from table Responses that meets a couple of conditions AND has the most recent date in the update_datetime field. (I actually want ALL the fields from the Responses row, but I'll settle for just its response_uuid.)
My attempt at this query, below, yields the error "An expression of non-boolean type specified in a context where a condition is expected, near ')'". What on earth am I doing wrong?
select response_uuid, MAX(update_datetime)
from Responses
where question_id=2115
and session_uuid in (
select session_uuid from Sessions
where client_uuid = '552782A2-4DC6-4715-B278-4C7F5F867975'
)
group by response_uuid
having MAX(update_datetime)
I've stared at similar questions all over the interwebs, and I'm just not seeing what the issue is. Thank you for any help!
p.s. This is actually a small part of a much larger query if you're interested in seeing the whole thing, or if seeing it all would be helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题就在这里:
这需要一个布尔表达式(逻辑上为真或为假的东西),例如
MAX(update_datetime) =
。你没有可以评估为真或假的表达式,因为你没有什么可比较的。从您的查询的外观来看(除非有我没有看到的东西),您可以完全删除
HAVING
。MAX
聚合上的GROUP BY
应该可以满足您的需求。 (为了清楚起见,我建议您使用诸如 MAX(update_datetime) AS MaxDateTime 之类的名称来命名结果列。)The problem is here:
This is expecting a boolean expression (something that is logically true or false), like
MAX(update_datetime) = <something>
. You have no expression it can evaluate as true or false, because you have nothing to compare.From the look of your query (unless there's something I'm not seeing), you can just remove the
HAVING
completely. TheGROUP BY
on aMAX
aggregate should give you what you want. (I'd suggest you name the resulting column using something likeMAX(update_datetime) AS MaxDateTime
for clarity.)