Mikhail Vega
Mikhail Vega

Reputation: 31

MySQL - sum values to unique ids

I have a table with file information, and I query -- SELECT DISTINCT File, Numbers FROM Table -- the table to create a two column table with the name of the file and some numbers i.e.

    File |  Numbers
    ---------------
    A    |    1
    A    |    2
    A    |    4
    B    |    3
    B    |    1
    B    |    2
    C    |    5
    C    |    3
    C    |    1

I am trying get this result, to sum up this query with the unique file name i.e.

    File |  Numbers
    ---------------
    A    |    7
    B    |    6
    C    |    9

I can get the individual sum according to the file name via SELECT File, SUM(Numbers) FROM (SELECT DISTINCT File, Numbers FROM Table) WHERE File = 'A'

but I want to have all three present in my results. I have tried - SELECT File, SUM(Numbers) FROM (SELECT DISTINCT File, Numbers FROM Table) but get the result of

    File |  Numbers
    ---------------
    C    |    22

Upvotes: 1

Views: 966

Answers (1)

potashin
potashin

Reputation: 44581

You need a group by:

select `File`, sum(`Numbers`) from `tbl` group  by `File`

Upvotes: 3

Related Questions