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 Tuesday, September 14, 2010 4:40 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Tuesday, November 5, 2013 4:40 PM
Points: 86, Visits: 131
I didnt get the exists condition here.. hw it works? can anyone explain me plz?

Thanks,
rahul
Post #985950
Posted Tuesday, September 14, 2010 10:29 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 7:52 AM
Points: 1,403, Visits: 1,814
rahul2671985 (9/14/2010)
I didnt get the exists condition here.. hw it works? can anyone explain me plz?

Thanks,
rahul


Rahul, please follow the entire discussion from page# 1, and I am sure you will get it. Essentially, both the WHERE and HAVING conditions are ultimately evaluated as boolean conditions - if the ultimate result is true, the particular record stays in the result set; else it is discarded.
What this question also illustrates very well is the logical query processing order, which is also clarified throughout the discussion.


Thanks & Regards,
Nakul Vachhrajani.
http://beyondrelational.com/modules/2/blogs/77/nakuls-blog.aspx
Be courteous. Drive responsibly.

Follow me on
Twitter: @sqltwins
Google Plus: +Nakul
Post #986014
Posted Wednesday, September 15, 2010 12:42 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 3:22 PM
Points: 5,925, Visits: 8,174
rahul2671985 (9/14/2010)
I didnt get the exists condition here.. hw it works? can anyone explain me plz?

Thanks,
rahul

I second Nakul's suggestion to first read the entire discussion. If, after that, you still struggle to understand the condition, then please post back and indicate exactly where you are stuck. I'll be happy to explain further.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #986043
Posted Wednesday, September 15, 2010 11:41 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Tuesday, November 5, 2013 4:40 PM
Points: 86, Visits: 131
Hi,

Thanks for replying me. I got stuck after "Having Exists".. the serch condition should take the groups which has max(a.thevalue) greater thaen 3. that I cannot figure out hw it works?

can u plz explain me in brief?

Rahul
Post #986541
Posted Wednesday, September 15, 2010 11:50 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:28 PM
Points: 13,103, Visits: 11,933
Go back and look at the whole thread. It has been explained in great detail with multiple scripts to run and test. Start on about 2 of the comments and read through the end of page 3.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #986549
Posted Wednesday, September 15, 2010 3:52 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 3:22 PM
Points: 5,925, Visits: 8,174
rahul2671985 (9/15/2010)
Hi,

Thanks for replying me. I got stuck after "Having Exists".. the serch condition should take the groups which has max(a.thevalue) greater thaen 3. that I cannot figure out hw it works?

can u plz explain me in brief?

Rahul

I'm not well known for my ability to explain "in brief" So I hope you don't mind a long explanation.

You first have to understand the basics of EXISTS. This condition is mostly used in a WHERE clause. For instance this query (which looks a bit like the one in the question, but is actually different):
SELECT a.GroupName, a.TheValue
FROM QotD AS a
WHERE EXISTS (SELECT *
FROM QotD AS b
WHERE b.TheValue > a.TheValue);

Logically speaking (the actual implementation can be different, but that is beyond our scope for now), SQL Server will process rows from table QotD (aliased as a) one by one. For each row, it will evaluate the subquery, thereby substituting the value of TheValue in the current row for a.TheValue. So for the "first" row (Group 1 / 1), the condition in the subquery will read "WHERE b.TheValue > 1", whereas for the "last" rows (Group 7 / 3), it will read "WHERE b.TheValue > 3".
After evaluating the subquery, SQL Server checks to see if any rows were returned. If that is the case, the EXISTS clause is considered to be true (there exists at least one row in the result of the subquery), the row passes the check for the conditions in the WHERE clause, so it will be included in the results of the query. If the result of the subquery is empty, the EXISTS condition evalutes to false (no row exists in the subquery) and the "current" row is discarded from the result set of the outer query.

You'll also have to understand the basics of HAVING. The HAVING clause is very similar to the WHERE clause. The only difference is that it does not operate on individual rows, but on groups of rows as defined by the GROUP BY clause. And that it includes or excludes those groups in their entirety based on the result of the condition evaluation.
In the case of the query in the original question, groups are made on GroupName - rows with the same GroupName are considered to be in the same group. Because of this grouping, it is still possible to refer to a.GroupName in the HAVING clause (as there can by definition be only one GroupName per group), but you can no longer access individual values of a.TheValue - there might be groups that happen to have only a single value in this column, but there is no guarantee and therefor the syntax rules forbid you to reference a.TheValue (as this would introduce the risk of an ambiguous reference). You can, however, include non grouped columns in an aggregate function. For each group, regardless of the number of rows it contains, MAX(a.TheValue), COUNT(DISTINCT a.TheValue), or AVG(a.TheValue) are an unambiguous reference that can evaluate to only one single value.
So in short, "HAVING a.TheValue = 2" is illegal (unless you add a.TheValue to the GROUP BY clause, but that would change the semantics); but "HAVING MAX(a.TheValue) = 2" is fine.

Once you know all these basics, understanding HAVING EXISTS is just a matter of combining the two. The HAVING implies that a condition has to be evaluated for each group (not for individual rows). The EXISTS then says that for each group, the subquery has to be evaluated. But references to the outer table have to be replaced by the values of the current group first. And bacause this is in a HAVING, a reference can only be to a column included in the GROUP BY, or to another column in an aggregate function.
In the question, MAX(a.TheValue) is the only reference. For the "first" group (Group 1), MAX(a.TheValue) is 3, so the subquery evaluated reads
EXISTS (SELECT * FROM QotD AS b WHERE b.TheValue > 3)

(NOTE: The "& gt;" in the code above should be a > symbol, but for some reason the site keeps changing it to the wacky & gt code)
If this subquery returns rows, the entire group is included in the result set of the outer query; if the subquery returns an empty set, the entire group is excluded from the result set.

And that's all. Basically, two concepts that are rather familier to most SQL Server developers, used in a very unfamiliar combination.

Does this help you figure out what happens?



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #986755
Posted Saturday, September 25, 2010 6:19 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Saturday, June 28, 2014 8:50 AM
Points: 2,649, Visits: 766
All I can say is WOW. Neat one!

Jamie
Post #993259
« Prev Topic | Next Topic »

Add to briefcase «««1234

Permissions Expand / Collapse