I have a postgresql table like this (but with 14 mln records)
organization inn income parameter
org1 111 10 apple
org2 222 4 pineapple
org1 111 6 orange
org1 111 7 pineapple
org1 111 3 orange
org2 222 8 apple
org2 222 1 orange
org1 111 9 pineapple
I need to calculate sum income for each organization, but also find the parameter with max sum income inside each organization
The result I want is following:
organization inn total_income max_parameter max_parameter_income
org 1 111 35 pineapple 16
org 2 222 13 apple 8
(actually, I have four parameter columns and I have to do similar thing with every parameter for each organization)
How could I do this?