Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


HAVING


HAVING

Author
Message
Rick Lang
Rick Lang
SSC Journeyman
SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)

Group: General Forum Members
Points: 86 Visits: 142
Perhaps a different version of the question where the answer would be different with the Having clause vs just the where clause might help to clear up any confusion (mine anyways)?
Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8319 Visits: 11548
Rick Lang (6/21/2010)
Perhaps a different version of the question where the answer would be different with the Having clause vs just the where clause might help to clear up any confusion (mine anyways)?
If you go back to page 4, you will see that I posted a script to create and populate the table. Copy and paste it into SSMS, run it, then try changing things and see how that affects the results.

If you then still have questions, feel free to ask!


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Rick Lang
Rick Lang
SSC Journeyman
SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)SSC Journeyman (86 reputation)

Group: General Forum Members
Points: 86 Visits: 142
Ooops, Sorry. Went on vacation and didn't read through the whole thread.
Paul White
Paul White
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10338 Visits: 11350
Rick Lang (6/21/2010)
Perhaps a different version of the question where the answer would be different with the Having clause vs just the where clause might help to clear up any confusion (mine anyways)?

In addition to Hugo's example, this is a favourite of mine:

SELECT 'One row'
WHERE 1 = 0
HAVING 0 = 0;





Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
John McC
John McC
Mr or Mrs. 500
Mr or Mrs. 500 (574 reputation)Mr or Mrs. 500 (574 reputation)Mr or Mrs. 500 (574 reputation)Mr or Mrs. 500 (574 reputation)Mr or Mrs. 500 (574 reputation)Mr or Mrs. 500 (574 reputation)Mr or Mrs. 500 (574 reputation)Mr or Mrs. 500 (574 reputation)

Group: General Forum Members
Points: 574 Visits: 1678
That's just plain wrong. ;-)

-------------------------------------------------------------------------
Normal chaos will be resumed as soon as possible. Crazy
Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8319 Visits: 11548
Paul White NZ (6/21/2010)
Rick Lang (6/21/2010)
Perhaps a different version of the question where the answer would be different with the Having clause vs just the where clause might help to clear up any confusion (mine anyways)?

In addition to Hugo's example, this is a favourite of mine:

SELECT 'One row'
WHERE 1 = 0
HAVING 0 = 0;


Wow!
You should have saved that for QotD, Paul!


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Paul White
Paul White
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10338 Visits: 11350
John McC (6/22/2010)
That's just plain wrong. ;-)

It still gives me nightmares from time to time Blink
I used to think I understood the basics of SQL.



Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
Toreador
Toreador
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2412 Visits: 8061
I've seen that one before somewhere.
It becomes slightly more obvious if you add a 'count(*)' to the select list
Paul White
Paul White
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10338 Visits: 11350
Hugo Kornelis (6/22/2010)
Wow!
You should have saved that for QotD, Paul!

Scary isn't it!

I blame a fellow MVP of yours: Rob Farley for all this - he reminded me of it in a T-SQL Tuesday blog a little while ago. I didn't have the link to hand before, but I've found it now, for anyone else that's interested:

http://msmvps.com/blogs/robfarley/archive/2010/01/12/t-sql-tuesday-having-puzzle-answer.aspx

I don't recall where I first came across it - it just seems to spring up every few years to remind me that SQL doesn't always go hand in hand with common sense...:-)



Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
Paul White
Paul White
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10338 Visits: 11350
Toreador (6/22/2010)
It becomes slightly more obvious if you add a 'count(*)' to the select list

Yep there's lots of variations:

SELECT 'One row', SUM(CASE WHEN 1 = 0 THEN 0 ELSE 1 END)
WHERE 1 = 0;





Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search