Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««1234»»

HAVING EXISTS Expand / Collapse
Author
Message
Posted Wednesday, September 8, 2010 8:48 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 10:24 AM
Points: 5,977, Visits: 8,239
Bradley Deem (9/8/2010)
In regards to your practice application example, assuming I understand the requirement, I believe there is a mistake in the having clause. See the comment in the first select statement.

You are absolutely right. Sorry for that mistake, and thanks for posting the corrected script.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #982366
Posted Wednesday, September 8, 2010 9:20 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, June 4, 2014 12:11 PM
Points: 16, Visits: 42
Good question, but there's an error in the explanation of the answer. At the end, it should say "with a maximum for TheValue of less than 3", not "less than 2".
Post #982404
Posted Wednesday, September 8, 2010 10:18 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 12:32 PM
Points: 1,316, Visits: 1,686
Tony Bater (9/8/2010)
In a recent blog, Kalen Delaney said she is frequently asked why is there a need to understand the internals of SQL Server. I think this question illustrates why. The key to getting the right answer is an understanding of the sequence of events during the query processing - the HAVING clause is processed after the GROUP BY and acts on the result set from the earlier phases.

I learned this from Itzik Ben-Gan's book 'Inside Microsoft SQL Server 2008: T-SQL Querying'. Chapter 1, Logical Query Processing, has a great section on the sequence of events that occurs when the query processor gets to work. I really recommend this book to anyone who is struggling to understand the explanation for this QotD.


Strictly speaking, this is not about the internals of SQL Server, but about the logical stucture of T-SQL. First and foremost, you need to understand the logical schema, because that's the only way you can write code that fits the schema, as well as tell if a given coding problem is your error or Microsoft's

The primary reason to understand the internals of SQL Server comes after that -- so you can know how to write queries that are not only logically correct, but that will perform well within the constraints of SQL Server and your environment.

In other words, you learn the logical process to write code that works, and then learn the actual internal process so you can write code that works well.

(Disclaimer: I got the question wrong, because I have much to learn about both subjects )
Post #982461
Posted Wednesday, September 8, 2010 10:25 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Friday, August 29, 2014 1:51 PM
Points: 21,644, Visits: 15,317
Excellent Question



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #982468
Posted Wednesday, September 8, 2010 12:42 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, May 19, 2011 7:26 AM
Points: 1,078, Visits: 289
WOW Hugo. Thanks for the question. Really had me thinking.
Post #982577
Posted Wednesday, September 8, 2010 3:07 PM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 3:43 PM
Points: 773, Visits: 1,172
That made my head swim. I was really confused after the explaination but as usual the discussion cleared it up. Thanks everyone.
Post #982686
Posted Wednesday, September 8, 2010 6:02 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, August 8, 2014 6:08 PM
Points: 872, Visits: 953
Great question! Thanks.
Post #982733
Posted Thursday, September 9, 2010 8:51 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, August 21, 2014 3:59 PM
Points: 534, Visits: 504
Great question!
Post #983182
Posted Thursday, September 9, 2010 3:23 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 8:24 AM
Points: 2,351, Visits: 2,700
dbowlin (9/8/2010)
QoTD has really been kicking my butt the last couple of weeks. I had to reread the explanation a couple of times to understand it. Good question, thanks.


Ditto. I'm getting some of them right, but Hugo really has my number. He posts some great questions, and invariably I get them wrong, but in this case I am going to have to study not only his original explanation but also the example he posted in the discussion (above). I don't understand it yet and I really want to.

Thanks, Hugo!

- webrunner


-------------------
"Operator! Give me the number for 911!" - Homer Simpson

"A SQL query walks into a bar and sees two tables. He walks up to them and says 'Can I join you?'"
Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html
Post #983474
Posted Saturday, September 11, 2010 1:53 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 6:40 PM
Points: 8,743, Visits: 9,292
Really good fun question. Thanks Hugo.

I looked at it for a bit and concluded that if it didn't generate an error message then it should return the three groups with no 3 in them. Would it generate an error? Well, why should it? Having needs a logical condition expression, so an "exists" expression should be OK as long as it obeys the rules for all logical condition expressions in a Having clause (ie don't refer to an non-aggregated column attribute of a row contributing to the group other that one of the grouping attributes). But it took me a while to convince myself that banning "Having Exists" would be silly, in fact I wasn't 100% certain so I was relieved when the website told me I had gotten it correct.


Tom
Post #984319
« Prev Topic | Next Topic »

Add to briefcase «««1234»»

Permissions Expand / Collapse