Solved

Update one table based on values in another. Complex query.

Posted on 2006-06-20
5
911 Views
Last Modified: 2007-12-19
I have an update that is hurting my brain. I have two tables:

loan (id, start, end, amount)
contrib ( id, date, payment)

for each row in loan there are one or more rows in contrib. Each entry in contrib is a payment on the loan. I need to update loan.start with the earliest date for the corresponding id in the contrib table, and update loan.end with the latest date found in the contrib table for that id.

So I need something like the following that actually works for all rows in loan:

update loan set start = (select min(date) from contrib where id = 'abc'),
  end = (select max(date) from contrib where id = 'abc') where id = 'abc'

0
Comment
Question by:jmarkfoley
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:acampoma
Comment Utility
Update loan set Start=MINDate, End=MaxDate from loan a Join
(select id,min(date)as MINDate, Max(date) as MaxDate from contrib group by Id) b
on a.Id=b.Id
0
 
LVL 6

Accepted Solution

by:
acampoma earned 500 total points
Comment Utility
Update loan set Start=MINDate, [End]=MaxDate from loan a Join
(select id,min(date)as MINDate, Max(date) as MaxDate from contrib group by Id) b
on a.Id=b.Id
0
 
LVL 5

Expert Comment

by:nicolasdiogo
Comment Utility
here is my take on the subject

update loan set

startDate = min( contrib.date ) ,
endDate = max( contrib.date )

from
  loan
, contrib

where

loan.id = contrib.id

let me know if it works

nicolas

www.brainpowered.net
0
 
LVL 1

Author Comment

by:jmarkfoley
Comment Utility
nicolasdiogo: yours didn't work. Got the error: An aggregate may not appear in the set list of an UPDATE statement.

 acampoma: yours worked!
0
 
LVL 5

Expert Comment

by:nicolasdiogo
Comment Utility
update loan set

  startDate = src.startDate
, endDate = src.endDate

from
(
select
  startDate = min( contrib.date )
, endDate = max( contrib.date )
from
  loan
, contrib
where
loan.id = contrib.id
) src

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Select2 jquery help 9 40
c# code 19 56
What's the difference between these two "qualifiers?" 3 26
separate column 24 19
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

762 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

8 Experts available now in Live!

Get 1:1 Help Now