Skip to content
Advertisement

Filter a condition after aggregate function in SQL Server?

I’ve got database of flights and I want it to return average of delay, but I want it to filter ‘origin = ‘SFO” after counting average of arr_delay_new. If I use WHERE or HAVING it counts the average with condition origin = ‘SFO’. How to make it count avg first and filter later?

This one has no condition origin = ‘SFO’

That’s a result of this, but there obviously are some lines with origin != ‘SFO’

If I use where or having:

Result of the second has the condition origin = ‘SFO’, but the average is counted with this condition (I want average of all flights not only with origin = ‘SFO’)

Advertisement

Answer

Use this HAVING clause:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement