How can i conditionally name a row in psql . If the h_te.to_pay_to_customer > h_te.pay_from_customer then i want to name the row to 'to_give_to_customer' if h_te.pay_from_customer > h_te.to_pay_to_customer then i want to name the row as 'to_receive_from_customer' else the name of the row should be balanced.
Here is what I tried:
SELECT u_c.name,
sum(CASE
WHEN h_te.to_pay_to_customer > h_te.pay_from_customer THEN
(h_te.to_pay_to_customer - h_te.pay_from_customer)
WHEN h_te.pay_from_customer > h_te.to_pay_to_customer THEN
(h_te.pay_from_customer - h_te.to_pay_to_customer)
ELSE 0
end) case when h_te.to_pay_to_customer > h_te.pay_from_customer then as to_give_to_customer WHEN h_te.pay_from_customer > h_te.to_pay_to_customer THEN as to_receive_from_customer else balanced,
u_c.owner_id
FROM home_transactionentries h_te
INNER JOIN home_transaction h_t
ON h_te.transaction_id = h_t.id
INNER JOIN users_customer u_c
ON u_c.id = h_t.customer_id
WHERE u_c.owner_id = 1
GROUP BY u_c.name,
u_c.owner_id;
but it is giving me a syntax error
ERROR: syntax error at or near "case"
LINE 8: end) case when h_te.to_pay_to_customer > h_te.pay_f...
Note:
h_te is home_transactionentries table
h_t is home_transaction table
u_c is user_customer table