我在查询一个列上有问题,差异值显示在PosgrestSql的2列结果中。显示值意味着突出,显示已经支付的0均值。并且需要显示所有待处理的列并计算已经支付的新列。我尝试了很多查询,但无法找到。
我的第一个查询如下: first query
然后我需要如下结果: I need the result
对于我的新查询,我尝试如下:
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
但仍然有错误:
错误:用作表达式
的子查询返回的多行
有没有专家帮我解决这个问题? 非常感谢你的帮助。
非常感谢, SmallCorner
答案 0 :(得分:1)
好吧,我不得不打破每个查询以了解发生了什么,所以这里实际上是你的主查询删除子查询并替换w / comments
主要查询
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;
未付款的分期付款
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;
付费分期付款
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;
我删除了INNER JOIN
,因为它与JOIN
相同。你应该尽力避免使用子查询(增加了复杂性,性能问题),但是如果你必须使用它们(我必须在工作中无数次)你应该自己运行它们以查看它们返回的结果。在你的情况下,我认为你甚至不能在select语句中使用子查询,因为它们需要绑定到你的同意。您必须将它们用作连接。无论哪种方式,这个答案都应该有效。
<强>答案强>
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;