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
 General SQL Server Forums
 New to SQL Server Programming
 How to add values of a column without using..
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

tennis_girl
Starting Member

40 Posts

Posted - 11/09/2008 :  12:12:24  Show Profile  Click to see tennis_girl's MSN Messenger address  Reply with Quote
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

India
52309 Posts

Posted - 11/09/2008 :  12:15:32  Show Profile  Reply with Quote
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

India
52309 Posts

Posted - 11/09/2008 :  12:18:51  Show Profile  Reply with Quote
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 - 11/09/2008 :  13:12:36  Show Profile  Click to see tennis_girl's MSN Messenger address  Reply with Quote
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

India
52309 Posts

Posted - 11/09/2008 :  13:18:32  Show Profile  Reply with Quote

DECLARE @SalaryTotal Numeric(12,2)

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

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

tennis_girl
Starting Member

40 Posts

Posted - 11/09/2008 :  19:01:15  Show Profile  Click to see tennis_girl's MSN Messenger address  Reply with Quote
what does the @ sign do?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 11/09/2008 :  23:00:15  Show Profile  Reply with Quote
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

India
22744 Posts

Posted - 11/10/2008 :  00:59:07  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
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

India
52309 Posts

Posted - 11/10/2008 :  01:09:56  Show Profile  Reply with Quote
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 - 11/10/2008 :  04:12:09  Show Profile  Click to see tennis_girl's MSN Messenger address  Reply with Quote
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 - 11/10/2008 :  04:14:27  Show Profile  Click to see tennis_girl's MSN Messenger address  Reply with Quote
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

India
52309 Posts

Posted - 11/10/2008 :  04:17:12  Show Profile  Reply with Quote
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 - 11/10/2008 :  05:29:13  Show Profile  Click to see tennis_girl's MSN Messenger address  Reply with Quote
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

India
52309 Posts

Posted - 11/10/2008 :  05:37:53  Show Profile  Reply with Quote
Ok...
Just told to indicate that solns provided will not always work fine in PostGre
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.11 seconds. Powered By: Snitz Forums 2000