SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 sum values alone or on the main query?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sapator
Constraint Violating Yak Guru

Greece
384 Posts

Posted - 01/17/2013 :  23:54:01  Show Profile  Reply with Quote
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
52317 Posts

Posted - 01/17/2013 :  23:57:25  Show Profile  Reply with Quote
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

Greece
384 Posts

Posted - 01/18/2013 :  23:17:37  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 01/19/2013 :  02:29:11  Show Profile  Reply with Quote
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

Greece
384 Posts

Posted - 01/19/2013 :  02:44:41  Show Profile  Reply with Quote
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
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 01/19/2013 :  02:56:20  Show Profile  Reply with Quote
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

Greece
384 Posts

Posted - 01/19/2013 :  03:48:28  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 01/19/2013 :  04:04:07  Show Profile  Reply with Quote
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

Greece
384 Posts

Posted - 01/19/2013 :  13:40:51  Show Profile  Reply with Quote
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
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000