Skip to content Skip to sidebar Skip to footer

Sql Function Calls Ignored With "having" Clause

For the below query, the MAX function seems to be completely ignored. The MAX function has no effect on the result of the query SELECT alarm_severity_id FROM alarm_notification WHE

Solution 1:

This is your query:

SELECT alarm_severity_id
FROM alarm_notification
WHERE alarm_life_cycle_id =25HAVINGMAX(event_timestamp);

Your HAVING clause is saying: take the maximum of the timestamp. If the maximum is not equal to zero or NULL, then everything is fine. This is exactly equivalent:

HAVING MAX(event_timestamp) <> 0

I suspect you want:

SELECT alarm_severity_id
FROM alarm_notification
WHERE alarm_life_cycle_id =25ORDERBY event_timestamp DESC
LIMIT 1;

EDIT:

To optimize performance, create an index on alarm_life_cycle_id and event_timestamp:

create index alarm_notification_alci_et on alarm_notification(alarm_life_cycle_id, event_timestamp)

Solution 2:

  1. There should be correct condition like having MAX(event_timestamp) > 100
  2. There should be group by if you use MAX
  3. Or do as @Gordon Linoff said (with order by + limit 1).

Solution 3:

Your query is incomplete, you need to use an operator and a value with the "having" statement, like

HAVINGMAX(event_timestamp) =100

What exactly are you trying to do?

Post a Comment for "Sql Function Calls Ignored With "having" Clause"