Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i get the sum of a database table to a variable?

I have a database table named Deposit and one of the column name in it is 30-12-2013. The column has two values such as 54,26.

i want to get the sum of that column column into a variable. I use the following code:

con.Open();
SqlCeCommand cmd11 = con.CreateCommand();
cmd11.CommandText = "select sum(30-12-2013) from Deposit";
int result = ((int)cmd11.ExecuteScalar());
displaylabel.Text = result.ToString();
con.Close();

But, I am getting the value of variable 'result' as -3990.

Whats wrong with my code.. Please help me.. Thanks in advance..

like image 255
Jayesh Babu Avatar asked Dec 30 '13 08:12

Jayesh Babu


People also ask

How do you assign a SUM to a variable in SQL?

SUM() and COUNT() functions SUM of values of a field or column of a SQL table, generated using SQL SUM() function can be stored in a variable or temporary column referred as alias. The same approach can be used with SQL COUNT() function too.

How do you use SUM in database?

You can use it to add all the values in one column across all rows in a table, to total the results of an expression that uses more than one column, and to sum up values for a group of rows. You can also use SUM() inside the HAVING clause to filter data according to the summed values.

Can you do SUM SUM in SQL?

The SQL Server SUM() function is an aggregate function that calculates the sum of all or distinct values in an expression. In this syntax: ALL instructs the SUM() function to return the sum of all values including duplicates.

How do I get the SUM of a count in SQL?

If you need to add a group of numbers in your table you can use the SUM function in SQL. This is the basic syntax: SELECT SUM(column_name) FROM table_name; If you need to arrange the data into groups, then you can use the GROUP BY clause.


1 Answers

(30-12-2013) * 2 (because you have two entries) = -1995 * 2 = -3990

You have to use:

SELECT sum([30-12-2013])
FROM   dbo.Deposit 
like image 98
Roman Pushkin Avatar answered Oct 04 '22 13:10

Roman Pushkin