June 22, 2010 at 1:53 am
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!
June 22, 2010 at 3:05 am
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.
June 22, 2010 at 3:09 am
I've seen that one before somewhere.
It becomes slightly more obvious if you add a 'count(*)' to the select list
June 22, 2010 at 3:11 am
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...:-)
June 22, 2010 at 3:17 am
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;
August 9, 2010 at 4:41 am
Hi,
Could not get the having clause ...
can anyonce please explain?
August 9, 2010 at 4:47 am
me_always (8/9/2010)
Hi,Could not get the having clause ...
can anyonce please explain?
Sure - but such a broad question is too hard to answer (other than with a reference to Books Online).
Have you read the explanation that is given after replying to the question? And have you read the discussion here?
If, after reading all that, you still have a question, I'll be happy to answer - as soon as you tell me what exactly you don't understand.
August 9, 2010 at 6:16 am
Learned something new about HAVING.
November 7, 2010 at 4:18 pm
Thanks, i was under the impression that we have to use Having with Group by only and also explanation is clear enough to understand how it works.
October 9, 2013 at 1:11 am
Excellent Question 🙂
Great discussion & explanations!
_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
October 9, 2013 at 1:32 am
THIS THREAD IS DEFINITELY GIVING A LOT TO LEARN.
SPECIALLY THIS:
SELECT 'One row'
WHERE 1 = 0
HAVING 0 = 0;
Viewing 11 posts - 46 through 55 (of 55 total)
You must be logged in to reply to this topic. Login to reply