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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 sum values alone or on the main query?

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 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.

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-19 : 02:29:11
so you want sum to be returned along with other columns?

can you provide sample data and required output in below format?

http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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,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.
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-19 : 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/

Go to Top of Page

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
Go to Top of Page
   

- Advertisement -