Solved

SQL Server 2005 script help

Posted on 2009-07-09
4
172 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
[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
  • 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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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