I’ve this table:
id value ref
1 0 10000
2 5 11000
3 5 11100
4 2 11101
5 2 11102
6 10 12000
... ... ....
How can i achieve this:
ref sum(value)
10000 14 <-- 5 + 5 + 2 + 2
11000 9 <-- 5 + 2 + 2
11100 4 <-- 2 + 2
12000 10 <-- and so on
I can’t decided if this desing is valid, it won’t work, or i’m missing something.
I don’t have any sql query to show to you guys.
Using regex, maybe? which i’d read about it, but not fully understand.
Any help would be greatly appreciated
This might work for you, after replacing
contentsumwith the real name of your table:However, its totals don’t match yours. Instead, they match what I thought you were adding up when I first read the question.
Basically, the key is to JOIN the table to itself on some function of the
refvalues, grouping by the left-hand side, and summing over the right-hand side. The subquery condenses identical rows and reduces some code duplication, but isn’t strictly necessary if therefvalues are all unique.