Solved

SQL Server 2005 script help

Posted on 2009-07-09
4
168 Views
Last Modified: 2012-05-07
Hi,

I have 2 tables one holding customer data, the other holding referral data, now I need to update a field based upon the referrals and this needs to be done nightly, for example

CUSTOMER (Table called contact2)
ACCOUNTNO, ULASTPROP

REFERRAL (Table called contsupp)
ACCOUNTNO, RECTYPE, DATE

Now what I need to do is make the ULASTPROP field equal to the newest date in the referral table using the accountno as the matching field. So if I take my customer entry, I may have 4 linked entries in referral table but I need the ULASTPROP to match the date of the LATEST referral.

Now I need to run through the entire customer database (approx 160,000) every night on an automated script, but im a bit stuck on where to proceeded.
0
Comment
Question by:DJMikeh
  • 2
4 Comments
 
LVL 37

Expert Comment

by:momi_sabag
ID: 24815399
update t1
set ulastprop = max(t2.date)
from customer t1 inner join referral t2 on t1.accountno = t2.accountno
0
 

Author Comment

by:DJMikeh
ID: 24821383
UPDATE t1 SET t1.ulastpropa = max(t2.lastdate)
FROM contact2 t1
INNER JOIN contsupp t2 ON t1.accountno = t2.accountno
WHERE t2.rectype='R'

When running the script above I get

An aggregate may not appear in the set list of an UPDATE statement.

Ideas? ive tried with and without the WHERE clause and it's the same each time.
0
 

Accepted Solution

by:
DJMikeh earned 0 total points
ID: 24821948
I have solved it myself using

UPDATE contact2 SET ulastpropa = (SELECT max(lastdate) FROM contsupp WHERE contact2.accountno = contsupp.accountno AND rectype='R')
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

830 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