dbTalk Databases Forums  

To sum them all

comp.databases.mysql comp.databases.mysql


Discuss To sum them all in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
José Carlos Santos
 
Posts: n/a

Default To sum them all - 07-29-2006 , 10:30 AM






Hi all:

Imagine that I have a table like this one:

---------
Quote:
a | 2 |
---------
b | 3 |
---------
a | 5 |
---------
c | 1 |
---------
c | 3 |
---------
a | 2 |
---------

The firts column has alphanumeric character and the second one
only has numbers. Let's call it test1. I want to create a table with the
same structure as the previous one which looks like this:

---------
Quote:
a | 9 |
---------
b | 3 |
---------
c | 4 |
---------

Let's call test2 to this table. The idea is this that:

1) The first column of test2 has the same "objects" in it as test1, but
each one appears only once.

2) For each "object", the number on the second column of test2 is the
sum of all numbers of the second column of test1 which have that
"object" in the first column.

Is it possible to do this within MySQL? I'm not talking about creating
the table, only about filling it up.

Best regards,

Jose Carlos Santos


Reply With Quote
  #2  
Old   
Giuseppe Maxia
 
Posts: n/a

Default Re: To sum them all - 07-29-2006 , 10:46 AM






José Carlos Santos wrote:
Quote:
Hi all:

Imagine that I have a table like this one:

---------
| a | 2 |
---------
| b | 3 |
---------
| a | 5 |
---------
| c | 1 |
---------
| c | 3 |
---------
| a | 2 |
---------

The firts column has alphanumeric character and the second one
only has numbers. Let's call it test1. I want to create a table with the
same structure as the previous one which looks like this:

---------
| a | 9 |
---------
| b | 3 |
---------
| c | 4 |
---------

Let's call test2 to this table. The idea is this that:

1) The first column of test2 has the same "objects" in it as test1, but
each one appears only once.

2) For each "object", the number on the second column of test2 is the
sum of all numbers of the second column of test1 which have that
"object" in the first column.

Is it possible to do this within MySQL? I'm not talking about creating
the table, only about filling it up.

Best regards,

Jose Carlos Santos
Have you ever heard of aggregate functions?
http://dev.mysql.com/doc/refman/5.0/...functions.html

Would this query meet your needs?
select first_column, sum(second_column)
from table_name
group by first_column

It's standard SQL.

ciao
gmax

--
_ _ _ _
(_|| | |(_|>< The Data Charmer
_|
http://datacharmer.org/


Reply With Quote
  #3  
Old   
José Carlos Santos
 
Posts: n/a

Default Re: To sum them all - 07-29-2006 , 11:00 AM



On 29-07-2006 16:46, Giuseppe Maxia wrote:

Quote:
Have you ever heard of aggregate functions?
http://dev.mysql.com/doc/refman/5.0/...functions.html
No, I'm a newbie. :-(

Quote:
Would this query meet your needs?
select first_column, sum(second_column)
from table_name
group by first_column

It's standard SQL.
I haven't tried it, but it really looks like you solved my problem.
Thanks a lot.

Best regards,

Jose Carlos Santos


Reply With Quote
Reply




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.