Solved

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

Posted on 2006-06-20
5
914 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
[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
  • 2
5 Comments
 
LVL 6

Expert Comment

by:acampoma
ID: 16946088
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
ID: 16946097
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
ID: 16947417
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
ID: 16947629
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
ID: 16949111
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

737 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