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
 General SQL Server Forums
 New to SQL Server Programming
 How to add values of a column without using..

Author  Topic 

tennis_girl
Starting Member

40 Posts

Posted - 2008-11-09 : 12:12:24
Hi,

Can anyone tell me how I can add values of a column without using any functions? I know I can do that with Sum.
but if I don't want to use it?

thanks

p.s. I'm new to SQL!!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-09 : 12:15:32
Not sure what you mean? do you mean adding all the values of column in a table? or add based on some grouping?can you illustrate your scenario please?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-09 : 12:18:51
if question was just to add up the values of column without any grouping applied and without use of SUM() function you can do it like this

DECLARE @ColSum int

SELECT @ColSum=COALESCE(@ColSum,0)+YourColumn
FROM YourTable

SELECT @ColSum--this will give your sum
Go to Top of Page

tennis_girl
Starting Member

40 Posts

Posted - 2008-11-09 : 13:12:36
visakh16,
it's you again :)
I'm not allowed to use any functions. so can't use the SUM function for example!
the question says: the salary total without using any functions (i.e. Sum or others...)
He has given us a table called employee with their names, salaries, etc.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-09 : 13:18:32
[code]
DECLARE @SalaryTotal Numeric(12,2)

SELECT @SalaryTotal=COALESCE(@SalaryTotal,0)+salary
FROM employee

SELECT @SalaryTotal--this will give your sum
[/code]
Go to Top of Page

tennis_girl
Starting Member

40 Posts

Posted - 2008-11-09 : 19:01:15
what does the @ sign do?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-09 : 23:00:15
quote:
Originally posted by tennis_girl

what does the @ sign do?


@ sign indicates its a local variable in SQL Server.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-11-10 : 00:59:07
quote:
Originally posted by tennis_girl

visakh16,
it's you again :)
I'm not allowed to use any functions. so can't use the SUM function for example!
the question says: the salary total without using any functions (i.e. Sum or others...)
He has given us a table called employee with their names, salaries, etc.


Why is the use of SUM not allowed?


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-10 : 01:09:56
quote:
Originally posted by madhivanan

quote:
Originally posted by tennis_girl

visakh16,
it's you again :)
I'm not allowed to use any functions. so can't use the SUM function for example!
the question says: the salary total without using any functions (i.e. Sum or others...)
He has given us a table called employee with their names, salaries, etc.


Why is the use of SUM not allowed?


Madhivanan

Failing to plan is Planning to fail


May be teacher hasnt taught aggregation functions chapter yet
Go to Top of Page

tennis_girl
Starting Member

40 Posts

Posted - 2008-11-10 : 04:12:09
The prof kinda sucks, so I'm just searching the commands on line.
He asked us to do it 2 ways, using a function and not using a function. that's the point of the excercise kinda!
Go to Top of Page

tennis_girl
Starting Member

40 Posts

Posted - 2008-11-10 : 04:14:27
quote:
Originally posted by visakh16

quote:
Originally posted by tennis_girl

what does the @ sign do?


@ sign indicates its a local variable in SQL Server.



We're using PostgreSQL (on linux!! grrr) for the time being and never seen using the @ sign.
I guess I'll skip that for now :)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-10 : 04:17:12
for postgreSQL specific solutions post in postgresql forums like www.dbforums.com. This is MS SQL Server forum so solutions here will be as per SQL Server syntax.
Go to Top of Page

tennis_girl
Starting Member

40 Posts

Posted - 2008-11-10 : 05:29:13
Visakh16, thanks for the tip.
I downloaded MS SQL server on my laptop to practice, remember? :)
so I make sure to ask specific qs here.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-10 : 05:37:53
Ok...
Just told to indicate that solns provided will not always work fine in PostGre
Go to Top of Page
   

- Advertisement -