开发者

What is wrong with this simple SQL Server 2008 R2 query? Yields "expression of non-boolean type ..." error

开发者 https://www.devze.com 2023-04-11 13:25 出处:网络
I am trying to choose the single row from table Responses that meets a couple of conditions AND has the most recent date开发者_StackOverflow中文版 in the update_datetime field.(I actually want ALL the

I am trying to choose the single row from table Responses that meets a couple of conditions AND has the most recent date开发者_StackOverflow中文版 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.


The problem is here:

having MAX(update_datetime)

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. The GROUP BY on a MAX aggregate should give you what you want. (I'd suggest you name the resulting column using something like MAX(update_datetime) AS MaxDateTime for clarity.)

0

精彩评论

暂无评论...
验证码 换一张
取 消