I have a table like below
| Column1 | Column2 |
|---|---|
| A | 200 |
| A | 200 |
| A | 0 |
| B | 300 |
| B | 200 |
| C | 100 |
I would like to transform this table into the following table
With calculation: for each element of column1, SUM (column2) / count of (non-zero column2)
| Column1 | Column2 |
|---|---|
| A | ((200+ 200 + 0) / 2) = 200 |
| B | ((300 + 200) / 2) = 250 |
| C | 100 / 1 = 100 |
The only thing I can think of is looping through distinct elements of Column1 and run:
SELECT SUM(Column2)
FROM Table
WHERE Column1 = i / (SELECT COUNT(Column2)
FROM Table
WHERE Column1 = i AND Column2 <> 0)
and generate a table.
Is there a better way of doing this?