Author |
Topic  |
|
sapator
Constraint Violating Yak Guru
Greece
462 Posts |
Posted - 01/17/2013 : 23:54:01
|
Hi. I want to sum some values from a result. My question is, does it make any difference if i sum the values in the original query "select something, (select sum....),somethingelse, ... etc , form table etc....." or it is better to create a new query with just the sum? The first one will bring something like ValueA,myValueA,sum ValueB,myValueB,sum ValueC,myValueC,sum so i would just need to take the some from whatever row(probably the first one), while the other will bring ValueA,myValueA ValueB,myValueB ValueC,myValueC and then i would run another query for the sum. Any thoughts? Thanks. |
Edited by - sapator on 01/17/2013 23:55:24
|
|
visakh16
Very Important crosS Applying yaK Herder
India
52326 Posts |
Posted - 01/17/2013 : 23:57:25
|
is sum to be aggregated at a different level than your other columns? ie is sum to be calculated for each combination of other two columns or you need a sum at higher level to be repeated for all other columns?
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
sapator
Constraint Violating Yak Guru
Greece
462 Posts |
Posted - 01/18/2013 : 23:17:37
|
Sum will be calculated for one column only, for all the rows of that column. I just went with 2 different queries since the Sproc was very large and i had to change a lot of code.But i would appreciate an opinion. Thanks. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
India
52326 Posts |
|
sapator
Constraint Violating Yak Guru
Greece
462 Posts |
Posted - 01/19/2013 : 02:44:41
|
I don't know if i want to.That is the thought i had. A simple data would be: Col1,Value,Sum A,1,20 B,10,20 C,8,20 D,1,20
So i'm asking if this is better than getting A,1 B,10 C,8 D,1
and then another query that will give you sum=20. |
Edited by - sapator on 01/19/2013 02:46:30 |
 |
|
visakh16
Very Important crosS Applying yaK Herder
India
52326 Posts |
Posted - 01/19/2013 : 02:56:20
|
that really depends on your requirement. Both ways can be achieved using sql queries
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
sapator
Constraint Violating Yak Guru
Greece
462 Posts |
Posted - 01/19/2013 : 03:48:28
|
Thanks.So it's not a loss to call sql 2 times for this situation? |
 |
|
visakh16
Very Important crosS Applying yaK Herder
India
52326 Posts |
Posted - 01/19/2013 : 04:04:07
|
you could simply do this
SELECT Col1,Value,SUM(Value) OVER() AS SumVal
FROM table
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
sapator
Constraint Violating Yak Guru
Greece
462 Posts |
Posted - 01/19/2013 : 13:40:51
|
Thanks.I'll keep that in mind, current sproc is too complicated (for me) and i have another ROW_NUMBER() OVER( to deal with, so i just went for a simple sum. But thanks |
Edited by - sapator on 01/19/2013 13:41:12 |
 |
|
|
Topic  |
|