?
Solved

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

Posted on 2006-06-20
5
Medium Priority
?
916 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Suggested Courses

741 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