Solved

SQL syntax - complicated where clause

Posted on 2011-09-23
7
269 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
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 51

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

910 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now