Skip to content
Advertisement

SQL Query: How to send MIN value from one table to another based off ID

Looking for help with SQL query!

I have 2 tables, A and B. Both tables have the column ‘student_id’ and ‘created_date’. For table A, ‘created_date’ is null but I wish to populate it with data from table B.

In table B, there are multiple of the same ‘student_id’ values, so I am trying to fetch only the one that has the earliest ‘created_date’.

Once I get that date, I want to then go back to table A, look for that exact same ‘student_id’ and then insert that MIN ‘created_date’ into table A.

If anybody could help that would be greatly appreciated!

Advertisement

Answer

You can make something like this

With the inner join you find the minimal Date in TableB and it is the right one for the student_id.

The rest is simple Updatelogic.

With the Where Clause at the end you can select only the rows that you need.

For Postgresql is the query like this

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement