SQL Syntax for adding multiple rows together from 1 column

This is a discussion on "SQL Syntax for adding multiple rows together from 1 column" within the Databases section. This forum, and the thread "SQL Syntax for adding multiple rows together from 1 column are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > Databases

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jan 17th, 2006, 00:28
New Member
Join Date: Jan 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Question SQL Syntax for adding multiple rows together from 1 column

Hello-

I want to add the column office_cost for all rows that are owned by a particular company. How can i do that? The column is a decimal 5,2 and needs to total all rows for that company so i can output a total cost.
Reply With Quote

  #2 (permalink)  
Old Jan 17th, 2006, 02:24
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: SQL Syntax for adding multiple rows together from 1 column

You should use a sum function on the column that you want to total up, and a group by clause to combine each bundle of records. Here's an example of houses for sale and the seller which totals up the value of properties for each selling agent.

Code: Select all
mysql> select agid, asking from sales;
+------+--------+
| agid | asking |
+------+--------+
|   10 | 225000 |
|   10 | 195000 |
|   10 | 237500 |
|    8 | 152000 |
|    8 | 205000 |
|    6 | 229950 |
|    5 | 335000 |
|    1 |  96950 |
|    5 | 279950 |
|    4 | 149950 |
|    1 | 220000 |
|    4 | 127500 |
|   11 | 465000 |
|    3 | 222000 |
|    3 | 465000 |
|    2 | 140000 |
|    2 | 116000 |
|   11 | 275000 |
|    4 | 250000 |
+------+--------+
19 rows in set (0.00 sec)

mysql> select agid, sum(asking) from sales group by agid;
+------+-------------+
| agid | sum(asking) |
+------+-------------+
|    1 |      316950 |
|    2 |      256000 |
|    3 |      687000 |
|    4 |      527450 |
|    5 |      614950 |
|    6 |      229950 |
|    8 |      357000 |
|   10 |      657500 |
|   11 |      740000 |
+------+-------------+
9 rows in set (0.40 sec)

mysql>
Reply With Quote
  #3 (permalink)  
Old Jan 17th, 2006, 03:54
New Member
Join Date: Jan 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Smile Re: SQL Syntax for adding multiple rows together from 1 column

Thanks, I got it!
Reply With Quote
  #4 (permalink)  
Old Jan 17th, 2006, 04:12
Junior Member
Join Date: Jan 2006
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Re: SQL Syntax for adding multiple rows together from 1 column

Code: Select all
sum(columname) group by columname
Reply With Quote
Reply

Tags
sql, syntax, adding, multiple, rows, together, column

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding 1 to Column Value Jack Franklin PHP Forum 4 Feb 3rd, 2008 10:19
Adding a column to a table Jack Franklin Databases 2 Feb 3rd, 2008 08:17
Adding Content to Multiple Pages longinwa Starting Out 5 Jun 19th, 2007 00:26
multiple rows => 1row??? ktsirig PHP Forum 1 Dec 31st, 2005 21:30
..copy data from column A in Table A to column B in Table B? gecastill Databases 10 Jun 23rd, 2005 18:27


All times are GMT. The time now is 19:56.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43