Professional Web Applications Themes

To sum them all - MySQL

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 ...

  1. #1

    Default To sum them all

    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
    Josť Carlos Santos Guest

  2. #2

    Default Re: To sum them all

    Josť Carlos Santos wrote:
    > 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?
    [url]http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html[/url]

    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
    _|
    [url]http://datacharmer.org/[/url]
    Giuseppe Maxia Guest

  3. #3

    Default Re: To sum them all

    On 29-07-2006 16:46, Giuseppe Maxia wrote:
    > Have you ever heard of aggregate functions?
    > [url]http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html[/url]
    No, I'm a newbie. :-(
    > 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
    Josť Carlos Santos Guest

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139