Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
amlwwalker
Starting Member
2 Posts |
Posted - 2012-05-10 : 16:00:22
|
| Hi Im (quite) new to sql programming, and im trying to write what I think should be quite a simple query.I have a table with groups of entries - lap times for consecutive laps for runners, for example:name timealex 3.25alex 3.28alex 3.31tim 3.21tim 3.30tim 3.25etc etc - its quite a large dataset. what i want to do is have another table, that merged them together so that for each runner i had one entry with the average time:alex 3.28tim 3.253my tables are called temp - which stores the orignial data, and temp2 will store the new data.i tried to run something like this:update temp2 set temp2.time=(select sum(temp.time)/3 from temp group by name) where temp2.name = temp.namebut that tells me it cant find temp.nameIs there a better way to do this?Thanks everyoneP.S Im using H2 database software which I dont think is that good but cant really change as my laptop is works and I dont have administrator rights.... |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-10 : 16:08:50
|
| [code]update t2set t2.time= t1.avgtime from temp2 t2inner join(select name,avg(time) as avgtime from temp group by name)t1 on t1.name = t2.name[/code][/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
amlwwalker
Starting Member
2 Posts |
Posted - 2012-05-10 : 16:21:44
|
| Thanks visakh16, but I dont quite understand:is t2 temp2 and t1 temp? |
 |
|
|
vijays3
Constraint Violating Yak Guru
354 Posts |
Posted - 2012-05-10 : 16:55:47
|
quote: Originally posted by amlwwalker Thanks visakh16, but I dont quite understand:is t2 temp2 and t1 temp?
T2 is a temp2 table here and t1 is derived table name that is (select name,avg(time) as avgtime from temp group by name) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-10 : 19:58:30
|
quote: Originally posted by amlwwalker Thanks visakh16, but I dont quite understand:is t2 temp2 and t1 temp?
they're table aliasesie shortnames given for Temp2 and derived table------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|