Author |
Topic |
sapator
Constraint Violating Yak Guru
462 Posts |
Posted - 2013-01-17 : 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 tableetc....."or it is better to create a new query with just the sum?The first one will bring something likeValueA,myValueA,sumValueB,myValueB,sumValueC,myValueC,sumso i would just need to take the some from whatever row(probably the first one), while the other will bringValueA,myValueAValueB,myValueBValueC,myValueCand then i would run another query for the sum.Any thoughts?Thanks. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-01-17 : 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 MVPhttp://visakhm.blogspot.com/ |
|
|
sapator
Constraint Violating Yak Guru
462 Posts |
Posted - 2013-01-18 : 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
52326 Posts |
|
sapator
Constraint Violating Yak Guru
462 Posts |
Posted - 2013-01-19 : 02:44:41
|
I don't know if i want to.That is the thought i had.A simple data would be:Col1,Value,SumA,1,20B,10,20C,8,20D,1,20So i'm asking if this is better than gettingA,1B,10C,8D,1and then another query that will give you sum=20. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-01-19 : 02:56:20
|
that really depends on your requirement. Both ways can be achieved using sql queries------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
sapator
Constraint Violating Yak Guru
462 Posts |
Posted - 2013-01-19 : 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
52326 Posts |
Posted - 2013-01-19 : 04:04:07
|
you could simply do thisSELECT Col1,Value,SUM(Value) OVER() AS SumValFROM table ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
sapator
Constraint Violating Yak Guru
462 Posts |
Posted - 2013-01-19 : 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 |
|
|
|