I got problem on query one column with difference value to display in 2 column result in PosgrestSql. The showing value mean outstanding, for the showing 0 mean already paid. And need to show all pending column and count the already paid in new column. I have tried many query but couldn't find.
My first query as below: first query
And then I need the result as below: I need the result
And for my new query that I try as below:
SELECT
m.customercd,
ta.agreementcd,
(SELECT
(tcd.balanceprincipal + tcd.balanceinterest + tcd.balanceapprovalfeeprincipal)
FROM t_agreement ta
INNER JOIN t_creditdetail tcd
ON ta.agreementcd = tcd.agreementcd
WHERE (tcd.balanceprincipal + tcd.balanceinterest + tcd.balanceapprovalfeeprincipal) != 0)
AS Installment,
(SELECT
(tcd.balanceprincipal + tcd.balanceinterest + tcd.balanceapprovalfeeprincipal)
FROM t_agreement ta
INNER JOIN t_creditdetail tcd
ON ta.agreementcd = tcd.agreementcd
WHERE (tcd.balanceprincipal + tcd.balanceinterest + tcd.balanceapprovalfeeprincipal) = 0)
AS Installment_PAID,
m.corpname,
m.employeenum
FROM t_agreement ta
INNER JOIN t_creditdetail tcd
ON ta.agreementcd = tcd.agreementcd
INNER JOIN m_customer m
ON ta.customercd = m.customercd
WHERE ta.agreementcd IN ('1502-0004759-9', '1506-0006177-9', '1506-0006327-8')
ORDER BY agreementcd
But still got error:
ERROR: more than one row returned by a sub query used as an expression
So are there any expert to help me on this? Would be very appreciate with your help.
Thank you very much, SmallCorner
Alright I had to break out each query to understand what is going on so here is effectively your main query with the sub queries removed and replaced w/ comments
main query
select
m.customercd,
ta.agreementcd,
# installment sub query
# installment paid sub-query
m.corpname,
m.employeenum
from t_agreement ta
JOIN t_creditdetail tcd ON ta.agreementcd = tcd.agreementcd
JOIN m_customer m ON ta.customercd = m.customercd
where ta.agreementcd in ('1502-0004759-9','1506-0006177-9','1506-0006327-8')
order by agreementcd;
unpaid installments
select
tcd.balanceprincipal + tcd.balanceinterest + tcd.balanceapprovalfeeprincipal AS installment
from t_agreement ta
JOIN t_creditdetail tcd ON ta.agreementcd = tcd.agreementcd
where
(tcd.balanceprincipal + tcd.balanceinterest + tcd.balanceapprovalfeeprincipal) != 0;
paid installments
select
tcd.balanceprincipal + tcd.balanceinterest + tcd.balanceapprovalfeeprincipal as installment_paid
from t_agreement ta
JOIN t_creditdetail tcd ON ta.agreementcd = tcd.agreementcd
where
(tcd.balanceprincipal + tcd.balanceinterest + tcd.balanceapprovalfeeprincipal) = 0;
I removed INNER JOIN
since it is the same as JOIN
. You should do your best to avoid using subqueries (added complexity, performance issues) but if you have to use them (I have to countless times at work) you should run them on their own to see what results they are returning. In your case I don't think you could of even used subqueries in the select statement as they need to be tied back to your agreemendcd. You would have to use them as joins. Either way this answer should work.
answer
select
m.customercd,
ta.agreementcd,
tcd.balanceprincipal + tcd.balanceinterest + tcd.balanceapprovalfeeprincipal AS balance,
m.corpname,
m.employeenum
from t_agreement ta
JOIN t_creditdetail tcd ON ta.agreementcd = tcd.agreementcd
JOIN m_customer m ON ta.customercd = m.customercd
where
ta.agreementcd in ('1502-0004759-9','1506-0006177-9','1506-0006327-8')
and (tcd.balanceprincipal + tcd.balanceinterest + tcd.balanceapprovalfeeprincipal) != 0 # unpaid balances
order by agreementcd;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.