I am using postgresql. How to get from first table to second? Thank you
id | type | sum
-----+------+-----
1 | a | 100
2 | a | 200
3 | b | 500
t_sum | type | history
-------+------+---------------
300 | a | ['id' => 1, 'sum' => 100], ['id' => 2, 'sum' => 200]
500 | b | ['id' => 3, 'sum' => 500]
I tried this, no results:
SELECT DISTINCT(a.type), SUM(a.sum) as t_sum, b.* as history FROM mytable a LEFT JOIN mytable b ON a.id = b.id GROUP BY a.type
DISTINCTis not a function, it's a part ofSELECT DISTINCT- and works in the whole selected rows. Not needed anyway since the GROUP BY returns no duplicates.