Solved

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

Posted on 2006-06-20
5
912 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
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

810 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