473,395 Members | 1,986 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

update field with max from another table

I have two tables STUDENTS and REGISTRATIONS. I wish to update my
STUDENT table field, LAST_ATTENDED_SESSION_ID from my REGISTRATION
table of the max SESSION_ID found in the REGISTRATION table.

STUDENT starts off with:
STUDENT_ID LAST_ATTENDED_SESSION_ID
123456789 000000
234567890 000000
345678901 000000

REGISTRATIONS:
STUDENT_ID SESSION_ID CLASS_ID
123456789 200001 BUS100
123456789 200102 BUS150
123456789 200203 BUS160

I want to update my STUDENT.LAST_ATTENDED_SESSION_ID with the
MAX(SESSION_ID) found in REGISTRATIONS for each student.
I'd end up with:
STUDENT_ID LAST_ATTENDED_SESSION_ID
123456789 200203
234567890 000000
345678901 000000
I've found examples here about selects and sub-queries, but nothing
yet in my situation with an update.
TIA
Rob
Jul 20 '05 #1
2 15151
Hi.

Maybe this could work:

update students
set last_attended_session_id = (select max(session_id)
from students as s
inner join registrations as r on
r.student_id = s.student_id
and students.student_id = s.student_id
)
Martin
Jul 20 '05 #2
On 15 Apr 2004 06:10:43 -0700, R Camarda wrote:
I have two tables STUDENTS and REGISTRATIONS. I wish to update my
STUDENT table field, LAST_ATTENDED_SESSION_ID from my REGISTRATION
table of the max SESSION_ID found in the REGISTRATION table.

STUDENT starts off with:
STUDENT_ID LAST_ATTENDED_SESSION_ID
123456789 000000
234567890 000000
345678901 000000

REGISTRATIONS:
STUDENT_ID SESSION_ID CLASS_ID
123456789 200001 BUS100
123456789 200102 BUS150
123456789 200203 BUS160

I want to update my STUDENT.LAST_ATTENDED_SESSION_ID with the
MAX(SESSION_ID) found in REGISTRATIONS for each student.
I'd end up with:
STUDENT_ID LAST_ATTENDED_SESSION_ID
123456789 200203
234567890 000000
345678901 000000
I've found examples here about selects and sub-queries, but nothing
yet in my situation with an update.
TIA
Rob


Hi Rob,

Without any DDL, I can do no more than take a wild guess at your
actual table structure, keys, constraints etc.

Maybe the following works:

UPDATE Students
SET Last_Attended_Session_ID =
(SELECT MAX(Session_ID)
FROM Registrations
WHERE Registrations.Student_ID = Students.Student_ID)
WHERE EXISTS
(SELECT *
FROM Registrations
WHERE Registrations.Student_ID = Students.Student_ID)
Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ike | last post by:
Is there a way in mysql to discern when the last date/time a field, row, or table was been updated or inserted? Thanks, ike
2
by: Mike Leahy | last post by:
Hello all, This question is related to updating tables - is there any way to calculate or update the values in a column in a table to the values in a field produced by a query result? An...
3
by: rrh | last post by:
I am trying to update a field in one table with data from another table. The problem I'm running into is I need to base the update on a range of data in the 2nd table. Table 1 has: date field...
4
by: N. Graves | last post by:
Hello... thank you for your time. I have a form that has a List box of equipotent records and a sub form that will show the data of the equipment select from the list box. Is it possible to...
8
by: Maxi | last post by:
There is a lotto system which picks 21 numbers every day out of 80 numbers. I have a table (name:Lotto) with 22 fields (name:Date,P1,P2....P21) Here is the structure and sample data: ...
15
by: Darren | last post by:
Help, i want to run an update query from a form.. and was wonderin.. Can the update query run if i want to update a value manually inputted from a form (e.g. !!) to a table...
2
by: Miro | last post by:
I will ask the question first then fumble thru trying to explain myself so i dont waste too much of your time. Question / Statement - Every mdb table needs a PrimaryKey ( or maybe an index - i...
13
by: shookim | last post by:
I don't care how one suggests I do it, but I've been searching for days on how to implement this concept. I'm trying to use some kind of grid control (doesn't have to be a grid control, whatever...
2
by: joeyrhyulz | last post by:
Hi, I'm trying to make a very simple update statement (in Oracle) in jet sql that seems much more difficult than it should be. The root of my problem is that I'm trying to update a field on a...
4
by: hapnendad | last post by:
In the question statement below Field names are in and variables are in (). All fields referenced are in what I have named the ‘PAR’ Table. Using MS Access 2003, I am working on a project...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.