I have the following DB structure.
| id | projectname | number | filename | type | unq | count |
|---|---|---|---|---|---|---|
| 8 | prj1 | 2 | a | t1 | 888389f661e117 | 1 |
| 9 | prj1 | 2 | a | t1 | 888389f661e117 | 2 |
| 10 | prj1 | 2 | a | t1 | 888389f661e117 | 2 |
| 11 | prj1 | 2 | a | t2 | 816418549711c3d33 | 6 |
| 12 | prj1 | 2 | a | t2 | 816418549711c3d33 | 7 |
| 13 | prj1 | 2 | a | t2 | 816418549711c3d33 | 1 |
| 14 | prj1 | 2 | a | t3 | NULL | NULL |
| 15 | prj1 | 2 | a | t3 | NULL | NULL |
| 16 | prj1 | 2 | a | t3 | NULL | NULL |
| 17 | prj1 | 36 | b | t1 | 8dac5bdffc7f86502 | 0 |
| 18 | prj1 | 36 | b | t1 | 8dac5bdffc7f86502 | 0 |
| 19 | prj1 | 36 | b | t1 | 8dac5bdffc7f86502 | 0 |
I use the query below to get the sums of count column w.r.t. the type column. A unique identifier of a row is `(projectname, number, filename).
SELECT DISTINCT ON (projectname, number, type) number, type, SUM(count) as count
FROM myTable
GROUP BY (projectname, number, type)
ORDER BY number
which gives me the output
| number | type | count |
|---|---|---|
| 2 | t1 | 5 |
| 2 | t2 | 14 |
| 2 | t3 | NULL |
| 36 | t1 | 0 |
| 36 | t2 | 16 |
| 36 | t3 | NULL |
My ideal output is: for every number column item, I want to divide the t2 value by t1 and the t3 value to t2. Can I accomplish this with Postgres commands without using external data manipulation techniques? I am looking to obtain a table as below. The type column is just representative of the operation I am interested in.
| number | type | ratio |
|---|---|---|
| 2 | t2t1 | 14 by 5 |
| 2 | t3t2 | NULL by 14 is NULL |
| 36 | t2t1 | 16 by 0 is INF |
| 36 | t3t2 | NULL by 16 is NULL |