Solved

SQL syntax - complicated where clause

Posted on 2011-09-23
7
278 Views
Last Modified: 2012-05-12
Hi experts,

I have two versions of code and I can't understand why they are different. The first updates the table for several thousand records, and the second only for 33. Can you help me understand why I can't accomplish the same thing in the second version of code? The difference is on lines 8 and 17, I tried to eliminate one where condition in line 8 with a join in line 17. Thanks!!
--First version works in setting the AA_Approved_Date to be the greatest available AA_Approved_Date from aa_approve_to_mail_lmo that is also smaller than the shipped date
update @table
set	aa_approved_date = dateadd(d,0,datediff(d,0,aa.approvaldate)),
	aa_approve_pool_name = aa.pool_name
from  @table t
inner join LMAdhoc.dbo.aa_approve_to_mail_lmo aa
on	t.acct# = aa.acct#
where dateadd(d,0,datediff(d,0,aa.approvaldate)) = (select max(dateadd(d,0,datediff(d,0,aa2.approvaldate))) from LMAdhoc.dbo.aa_approve_to_mail_lmo aa2 where aa2.acct# = aa.acct# and (dateadd(d,0,datediff(d,0,aa2.approvaldate)) <= t.shipped_date))

--Second version: Why does this update less values for aa_approved_date? 
update @table
set	aa_approved_date = dateadd(d,0,datediff(d,0,aa.approvaldate)),
	aa_approve_pool_name = aa.pool_name
from  @table t
inner join LMAdhoc.dbo.aa_approve_to_mail_lmo aa
on	t.acct# = aa.acct#
where dateadd(d,0,datediff(d,0,aa.approvaldate)) = (select max(dateadd(d,0,datediff(d,0,aa2.approvaldate))) from LMAdhoc.dbo.aa_approve_to_mail_lmo aa2 inner join @table t on aa2.acct#=t.acct# where (dateadd(d,0,datediff(d,0,aa2.approvaldate)) <= t.shipped_date))

Open in new window

0
Comment
Question by:JC_Lives
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 8

Expert Comment

by:Crashman
ID: 36589954
the first use a where

where    aa2.acct# = aa.acct#
                                                                    and ( dateadd(d, 0, datediff(d, 0, aa2.approvaldate)) <= t.shipped_date )
                                                         )

Open in new window


The second has a inner to @table

inner join @table t on aa2.acct# = t.acct#
                                                           where    ( dateadd(d, 0, datediff(d, 0, aa2.approvaldate)) <= t.shipped_date )

Open in new window


in the first you make a join in the where with the external query aa to the internal aa2
in the second you make directly the join to the table @table
maybe this is the problem
0
 
LVL 56

Expert Comment

by:HainKurt
ID: 36590029
in the second you are using @table t twice and the second is not the same as first one

to me, second is wrong (logically)...
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 36593079
the first has a correlated subquery ... the value of the subquery is related to the current row being processed by the
outer statement.

In the second the subquery is uncorrelated so the value returned is independant of the row being processed.

in the second you are only updating where the date is the max date on the table...
in the first you are updating rows on the table where for that "row set" its date is the maximum...

0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 total points
ID: 36593092
you'd need to write the second like this for it to be equivalent to the first

update @table
set	aa_approved_date = dateadd(d,0,datediff(d,0,aa.approvaldate)),
	aa_approve_pool_name = aa.pool_name
from  @table t
inner join LMAdhoc.dbo.aa_approve_to_mail_lmo aa
on	t.acct# = aa.acct#
where dateadd(d,0,datediff(d,0,aa.approvaldate)) 
  = (select max(dateadd(d,0,datediff(d,0,aa2.approvaldate))) 
       from LMAdhoc.dbo.aa_approve_to_mail_lmo aa2 
      inner join @table t1 
         on aa2.acct#=t1.acct# 
      where (dateadd(d,0,datediff(d,0,aa2.approvaldate)) <= t1.shipped_date)
       and aa2.acct#=aa.acct#
       and t.shipped_date=t1.shipped_date
     )

Open in new window

0
 

Author Closing Comment

by:JC_Lives
ID: 36598850
Thanks lowfatspread, that was super helpful! And just curious, do you happen to have a definition handy for a correlated subquery? Thanks!!
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 36601387
a correlated subquery is one where the values returned are directly related to the row being processed in the outer query
0
 

Author Comment

by:JC_Lives
ID: 36601535
cool, thanks
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question