|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 4:24 AM
Points: 5,232,
Visits: 7,022
|
|
Chris Cradock (9/1/2010) I'm not attempting to say HAVING without GROUP BY is invalid, and yes adding MAX(col2) to the selected columns I would then expect the behaviour you describe (and I would also reason the challenge would have been a "doddle" for everyone as it would then be obvious what HAVING was up to).
Its the expectation that HAVING has access to anything that wasn't explicitly worked out at the record selection stage, and thus have access to the MAX(col2) value. SQL server essentially extends the columns selected to resolve the HAVING MAX(col2). So it performs the statement in your response. But that's not in the SQL standard as far as I'm aware.
I hope that clarifies my statement. Ah, I think I understand. So does this mean that you would have had the same objection if I had included a GROUP BY clause?
SELECT COUNT(*) FROM QotD WHERE Col2 <> 4 GROUP BY Col1 HAVING MAX(Col2) < 5; Your misunderstanding is understandable, because we humans are "trained" to read bottom to top (unless you were raised in a culture that writes in a different direction, obviously).
SQL should not be interpreted that way. The logical expression order of the clauses in a query is: 1. FROM clause (including all joins), to find (and combine) the table(s) worked on; 2. WHERE clause, to throw out individual non-qualifying rows; 3. GROUP BY clause, to combine remaining rows to groups; 4. HAVING clause, to throw out complete non-qualifying groups; 5. SELECT clause, to form the columns in the result set from the columns; 6. ORDER BY, to transform the result from an unordered set to an ordered cursor.
Because this is the logical order of evaluation, expression in the WHERE, GROUP BY, and HAVING clause can not reference the results of expressions in the SELECT clause, but ORDER BY can (so if you have SELECT Col1 + Col2 AS TheSum, you can use TheSum in the ORDER BY clause but nowhere else). The ANSI standard also says the reverse (that expressions not used in the SELECT clause can not be used in the ORDER BY clause), but SQL Server does allow this. That is a case that you could think of as SQL Server "secretly" adding an extra column to the SELECT list. But again, that is for ORDER BY, not for HAVING.
I hope this clarifies the issue. If not, then please don't hesitate to ask further questions!
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: 2 days ago @ 6:54 AM
Points: 9,364,
Visits: 6,462
|
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 4:24 AM
Points: 5,232,
Visits: 7,022
|
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: 2 days ago @ 6:54 AM
Points: 9,364,
Visits: 6,462
|
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Wednesday, April 10, 2013 3:04 PM
Points: 839,
Visits: 938
|
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, September 18, 2012 4:53 AM
Points: 123,
Visits: 28
|
|
Excellent question. I was not aware of this and I gave a wrong answer
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 6:35 AM
Points: 7,078,
Visits: 7,119
|
|
This was real fun. So was the subsequent discussion. I really ought to get back to looking at QOTD regularly as there seem to be some real gems around now instead of the dire stuff of 6 months ago.
I got the right answer by applying a logically wrong method which I've used before (pretend that any aggregates in the having clause but not in the select clause are added to the select clause, that the having clause then operates on the result of the combined select and group by clauses, and that after executing the having clause any spurious additions to the select clause are discarded so that the result has only the required columns). Although this is far from what logically happens it usually delivers the same results (if I've read the standard correcly, that "usually" is actually "always", so that doing it that way - if it were efficient - would be a valid option for an optimizer).
Tom Que conclure à la fin de tous mes longs propos? C'est que les préjugés sont la raison des sots. (Voltaire, 1756)
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 9:38 AM
Points: 385,
Visits: 227
|
|
It is real joy to answer such questions. I was able to look at so usual command from another point, turn on common logic and get it right. But I was not sure I was right. Thanks!
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 10:33 AM
Points: 10,989,
Visits: 10,529
|
|
Excellent question Hugo - though I too chose the wrong answer.
Paul White SQL Server MVP SQLblog.com @SQL_Kiwi
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: 2 days ago @ 1:06 PM
Points: 469,
Visits: 192
|
|
| Great question and analogy on the HAVING... GROUP BY.
|
|
|
|