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

Group by and Where clause Expand / Collapse
Author
Message
Posted Sunday, March 3, 2013 9:47 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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
Posted Sunday, March 3, 2013 8:33 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:34 PM
Points: 36,952, Visits: 31,461
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 "Row-By-Agonizing-Row".

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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1426065
Posted Sunday, March 3, 2013 11:04 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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
Posted Monday, March 4, 2013 12:43 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 9:36 AM
Points: 42,765, Visits: 35,863
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 »

Add to briefcase

Permissions Expand / Collapse