Solved

SQL Server 2005 script help

Posted on 2009-07-09
4
158 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

746 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

9 Experts available now in Live!

Get 1:1 Help Now