Okay so I start with this query that works well:
SELECT org.organization_id, org.comp_code, org.name, cust.name as customer,
o.order_number as number, o.order_id as id, cast(o.total_charge as real) as receivable
FROM
organization as org, orders as o, organization as cust,
reconcile_order as ro
WHERE org.organization_id = o.shipper_org_id
and o.broker_org_id = cust.organization_id
and o.order_id = ro.order_id
and o.status = 'D'
and org.comp_code = 'ABC'
and (ro.receive_payment_in_full = 0 or ro.receive_payment_in_full is NULL)
But now I want add a sum from another table for each order_id. This other table is called reconcile_receivables and it has a column called amount. But the trick is that multiple amounts can relate to a unique order_id. So, I need to sum these amounts for every unique order. This is what I tried:
SELECT org.organization_id, org.comp_code, org.name, cust.name as customer,
o.order_number as number, o.order_id as id, cast(o.total_charge as real) as receivable,
sum(rr.amount) as partial_pay
FROM
organization as org, orders as o, organization as cust,
reconcile_order as ro, reconcile_receivables as rr
WHERE org.organization_id = o.shipper_org_id
and o.broker_org_id = cust.organization_id
and o.order_id = ro.order_id
and o.order_id = rr.order_id
and o.status = 'D'
and org.comp_code = 'ABC'
and (ro.receive_payment_in_full = 0 or ro.receive_payment_in_full is NULL)
My problem is I get this error:
column "org.organization_id" must appear in the GROUP BY clause
or be used in an aggregate function
Adding a group by clause doesn't help. So how can I solve this to show a column that represents the sum of every rr.amount related to each o.order_id? For example, there are two rr.amount values whose sum is 1000 and they relate to order_id = 3. So for that row, it should show sum amount column with the value of 1000
Group by