How do I get data from multiple other entries in a database?
July 17, 2011 2:43 PM Subscribe
SQL help: I'm very new to SQL, and need to update one field with the maximum value of all entries in another field selected based on a shared value in a third field.
I have a table like this:
A |B |C
1 |5 |
1 |4 |
2 |0 |
2 |2 |
3 |3 |
3 |1 |
I need to update field C to contain the maximum value of field B for all entries that share the same value in field A.
That is:
A |B |C
1 |5 |5
1 |4 |5
2 |0 |2
2 |2 |2
3 |3 |3
3 |1 |3
I know I need to be doing an update on C and a select based on A and a max of B from that selection, or something, but I have no idea what the syntax for this should look like.
Help?
Relevant: I am trying to do this in Open Office Base.
I am totally new to SQL, and would do this in a spreadsheet program if I thought I could. My impression is that SQL is the right solution for this problem, and that I should probably learn it.
posted by agentofselection to computers & internet (12 answers total) 4 users marked this as a favorite
create table temp like table_name;
insert into temp select * from table_name;
update table_name set C=(select max(B) from temp where temp.A=table_name.A);
drop table temp;
posted by babbyʼ); Drop table users; -- at 2:51 PM on July 17, 2011