Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2008
»
SQL Server Newbies
»
Group by and Where clause
Group by and Where clause
Rate Topic
Display Mode
Topic Options
Author
Message
sanandh87
sanandh87
Posted Sunday, March 03, 2013 9:47 AM
Grasshopper
Group: General Forum Members
Last Login: Friday, March 15, 2013 1:01 AM
Points: 24,
Visits: 51
Hi,
Please see below,
Table name : Log_table
Columns: Logid int, databasename varchar(30), tablename varchar(30), logdate datetime
Query 1:
Select max(logdate) from log_table
where databasename='db1' and tablename='table1';
consider the Log_table is empty, when I execute Query 1, the result will be 'Null'.
Query 2 :
select max(logdate) from log_table
Where databasename='db1' and tablename='table1'
group by databasename,tablename;
Consider the log_table is empty, when I execute Query 2, the result will be empty.
Note : both the condition table is empty.
Please explain me what happening inside and why the result different?
Post #1425968
Jeff Moden
Jeff Moden
Posted Sunday, March 03, 2013 8:33 PM
SSC-Dedicated
Group: General Forum Members
Last Login: Today @ 9:40 AM
Points: 32,894,
Visits: 26,774
In the first query, you give SQL Server no choice. You say to find the MAX unconditionally for the given criteria and there isn't one so it returns a NULL.
In the second query, it's different. You're saying find the MAX for a given group which is actually a condition. There are no groups that match your criteria so it returns nothing.
--Jeff Moden
"
RBAR
is pronounced "ree-bar" and is a "Modenism" for "
R
ow-
B
y-
A
gonizing-
R
ow".
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1426065
sanandh87
sanandh87
Posted Sunday, March 03, 2013 11:04 PM
Grasshopper
Group: General Forum Members
Last Login: Friday, March 15, 2013 1:01 AM
Points: 24,
Visits: 51
Thanks for your reply Jeff.
I got clear pic .. thanks a lot...
one more doubt Jeff,
both the condition table is empty,
then why the where condition returns 'null'
and
group by condition returns nothing.
can you plz explain me ?
Post #1426089
GilaMonster
GilaMonster
Posted Monday, March 04, 2013 12:43 AM
SSC-Dedicated
Group: General Forum Members
Last Login: Today @ 10:16 AM
Points: 37,663,
Visits: 29,915
http://sqlblog.com/blogs/paul_white/archive/2012/03/12/fun-with-aggregates.aspx
Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild
: Discussions on DB performance with occasional diversions into recoverability
We walk in the dark places no others will enter
We stand on the bridge and no one may pass
Post #1426104
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.