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 |
cable_si
Starting Member
20 Posts |
Posted - 2011-11-09 : 11:10:40
|
Hii am having problems with a query i havei am trying to do a select updatei want to have something likeupdate my_tableset dur = (SELECT@rec_count = (select count(*) from my_table),@sum_dur = (select sum(dur) from my_table),@averagedur = (@sum_dur / @rec_count))so dur is = @averagedurthe problem is my select statement is a bit off, as i am returning 3 values when i only want onehope that makes sensethanksSimon |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-09 : 11:27:01
|
[code]update my_tableset dur = (select count(*) from my_table)*1.0/(select sum(dur) from my_table)[/code]are you aware that this will set same value for Dur field in all rows?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
cable_si
Starting Member
20 Posts |
Posted - 2011-11-09 : 11:37:25
|
quote: Originally posted by visakh16are you aware that this will set same value for Dur field in all rows?
i am yes thanksintrestingly when i add further constraints to the query the result becomes 0,ie. select count(*) from my_table where field1 = 'Hello')*1.0/(select sum(dur) from my_table where field1 = 'Hello'any ideasthanksSimon |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-09 : 11:39:22
|
whats the datatype of dur?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
cable_si
Starting Member
20 Posts |
Posted - 2011-11-09 : 11:40:34
|
quote: Originally posted by visakh16 whats the datatype of dur?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
hiits an intthanks |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-09 : 12:43:13
|
quote: Originally posted by cable_si
quote: Originally posted by visakh16 whats the datatype of dur?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
hiits an intthanks
then it wont be able to store decimal resulteither make it decimal/numeric or apply cieling to round it to nearest integerupdate my_tableset dur = cieling( (select count(*) from my_table)*1.0/(select sum(dur) from my_table)) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|