|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Adding Values in a column
I have searched the forums and can't seem to find out how to do this. I have a set of values in a column in a database, I want to add all those values up then display them, could anyone help me out.
Thanks |
|
#2
|
|||
|
|||
|
Ok what should I assume here...right nothing!
1) Build an SQL Query with the records you wish to display 2) Create a Connection Object that connects to your database. 3) Execute your SQL query using your Connection Object, this will give/create you an *implicit* Recordset object holding all the values from your SQL Query 4) Start looping using your Recordset Object displaying *at the same time* the data in a nicely formatted html <table> 5) Close and Free the Recordset and Connection Object 6) That's it! Now...do you want a code example? If so, then what database are you using? What version of IIS/ASP are you using? What's your SERVER-SIDE language your using? Otherwise, with which part are you having problems? a) Creating the SQL Query b) Creating the Connection Object c) Executing the SQL Query d) Looping and creating a nicely formatted html <table> e) All of the above! Hope this helps! Sincerely Vlince |
|
#3
|
|||
|
|||
|
I know how to do all that a-e I want to add all the values together in a single column they are numbers, I just want to add all the numbers in one column up.
Last edited by gxblueknight : September 10th, 2003 at 12:16 PM. |
|
#4
|
|||
|
|||
|
Oh..............hehe
Why not do it SQL Style with the COUNT() Keyword Try that no? Hope this helps! Sincerely Vlince |
|
#5
|
|||
|
|||
|
Well I would but I am not sure how to do that could you give me a code example
Thanks alot |
|
#6
|
|||
|
|||
|
Just so you'll know, this has nothing to do with your ASP page.
In fact, it's part of the SQL language. So what you'll want is to create an SQL Query that COUNTs the total of your field in question. Now as for an example, I'll copy/paste the books online of SQL Server: **************************************************** Examples A. Use COUNT and DISTINCT This example finds the number of different cities in which authors live. USE pubs GO SELECT COUNT(DISTINCT city) FROM authors GO Here is the result set: ----------- 16 (1 row(s) affected) B. Use COUNT(*) This example finds the total number of books and titles. USE pubs GO SELECT COUNT(*) FROM titles GO Here is the result set: ----------- 18 (1 row(s) affected) C. Use COUNT(*) with other aggregates The example shows that COUNT(*) can be combined with other aggregate functions in the select list. USE pubs GO SELECT COUNT(*), AVG(price) FROM titles WHERE advance > $1000 GO Here is the result set: ----------- -------------------------- 15 14.42 (1 row(s) affected) **************************************************** Hope this helps! Sincerely Vlince |
|
#7
|
||||
|
||||
|
I think maybe the more suitable aggregate function of SQL would be the SUM finction which adds up all the non null values in a column, like:
sql = "SELECT SUM(Quantity) as QuantitySold, SUM(Quantity*UnitPrice) as ValueSold FROM Orders" (the second part is an example of an additional function of the SUM using a calculation) COUNT() would give the number of Orders, but SUM() would give the numeric values added together. You could do it by getting COUNT() and AVG() and multiplying them, I suppose, but am not sure it would match perfectly, and it would mean more work anyway ![]() |
|
#8
|
|||
|
|||
|
yeah i used sum and it worked, thanks guys.
|
|
#9
|
|||
|
|||
|
Yes OldJacques's right!
Pfffff...COUNT() c'mon, how foolish of me! Vlince |
|
#10
|
|||
|
|||
|
amateur
![]() |
|
#11
|
|||
|
|||
|
lol
|
|
#12
|
|||
|
|||
|
Hehe...
![]() Yes well aren't we all! Thanks ![]() Vlince |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ASP Programming > Adding Values in a column |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|