Conditional sum (sumif) in org-table

I have a table like this:

#+NAME: ENTRY
|------+--------|
| Item | Amount |
|------+--------|
| A    |    100 |
| B    |     20 |
| A    |    120 |
| C    |     40 |
| B    |     50 |
| A    |     20 |
| C    |     16 |
|------+--------|

and then I need to sum each element in a different table:

#+NAME: RESULT
|------+-----|
| Item | Sum |
|------+-----|
| A    | 240 |
| B    |  70 |
| C    |  56 |
|------+-----|

I tried using vlookup and the remote link in this table, but I can’t summarize the resulting list, for example:

#+TBLFM: $2=vsum((vconcat (org-lookup-all $1 '(remote(ENTRY,@2$1..@>$1)) '(remote(ENTRY,@2$2..@>$2)))))

But he does not give an answer.

So, I have to use a place holder to store the resulting list, and then sum it up:

#+NAME: RESULT
|------+--------------+-----|
| Item | Placeholder  | Sum |
|------+--------------+-----|
| A    | [100 120 20] | 240 |
| B    | [20 50]      |  70 |
| C    | [40 16]      |  56 |
|------+--------------+-----|
#+TBLFM: $2='(vconcat (org-lookup-all $1 '(remote(ENTRY,@2$1..@>$1)) '(remote(ENTRY,@2$2..@>$2))))::$3=vsum($2)

Is there a better solution for this?

+4
source share
1 answer

One way to do this is vsum:

#+TBLFM: $2='(apply '+ (mapcar 'string-to-number (org-lookup-all $1 '(remote(ENTRY,@2$1..@>$1)) '(remote(ENTRY,@2$2..@>$2)))))

If you want to use the calc function, you can always use calc-eval:

#+TBLFM: $2='(calc-eval (format "vsum(%s)" (vconcat (org-lookup-all $1 '(remote(ENTRY,@2$1..@>$1)) '(remote(ENTRY,@2$2..@>$2))))))
+2
source

All Articles