1

I have this

id    value
-----!-----
 1      3
 2      3
 1      2
 1      1

i have tried using

SELECT id,sum(value) FROM table GROUP BY id 

but it shows something different.

   id   ! value
  --------------   
   1        6
   2        3 

And i want to add values based on their id's but keeping id's there

 id   ! value
--------------   
 1      6
 2      3
 1      6
 1      6

Please help Thank you

0

2 Answers 2

4

Are you looking for this?

SELECT t.id, q.value
  FROM Table1 t JOIN
(
  SELECT id, SUM(value) value
    FROM Table1
   GROUP BY id
) q ON t.id = q.id

Output:

| ID | VALUE |
|----|-------|
|  1 |     6 |
|  2 |     3 |
|  1 |     6 |
|  1 |     6 |

Here is SQLFiddle demo

Sign up to request clarification or add additional context in comments.

Comments

1

The naive approach would be :

select id, (select sum(value) from tbl where id = t1.id) value
from tbl t1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.