Have aggregate function group results in one row I have a table that has the following columns: 


debit
credit 
payment_method
class 


Now I will perform this query: 

 select sum(debit - credit), payment_method, class 
 from my_table 
 group by payment_method, class 


And since I have various payment methods and various classes, I will have several rows of the result set. 

Now I want to be able to aggregate this result and have the output grouped by class only, for example for a certain class 001 I want to have: 
payment_method1, sumofpayment_method1, payment_method2, sumofpayment_method2, payment_method3, sumofpayment_method3 in one row. 

Is it possible in Postgres 9.1?   

Update: 

The table definition is as follows:-


debit numeric(10,2)
credit numeric(10,2)
payment_method varchar(20)
class varchar(25)
You are looking for the crosstab() function provided by the additional module tablefunc.

Then your function could look like this:

SELECT * FROM crosstab(
   'SELECT class, payment_method, sum(debit - credit) AS saldo
    FROM   tbl
    GROUP  BY 1,2
    ORDER  BY 1,2'

   ,$$VALUES 
        ('payment_method1'::text), ('sumofpayment_method1')
      , ('payment_method2'), ('sumofpayment_method2')
      , ('payment_method3'), ('sumofpayment_method3')$$
   )
AS ct (class text
     , payment_method1 numeric, sumofpayment_method1 numeric
     , payment_method2 numeric, sumofpayment_method2 numeric
     , payment_method3 numeric, sumofpayment_method3 numeric);


I am using all 6 "types" you mentioned. Well, I see that only half of them are actually intended as type. Either way, substitute your actual types.
More details, links and explanations in this related answer on SO.