我无法返回包含2个不同行的值
acct # | Loan 1 |Parent Loan|Variable
-------|-------- |-----------|-----------
1 | 1 |1 |Fixed
1 | 2 |1 |Variable
2 | 1 |1 |Fixed
3 | 1 |1 |Variable
4 | 1 |1 |Variable
被驱逐的结果将是
帐户1父贷款1 =混合
帐户2将被修复
帐户3和4将是可变的
答案 0 :(得分:0)
根据您在上述评论中提供的其他信息,此可能是您正在寻找的答案。如果没有,那么我希望它很接近。
您需要计算DISTINCT贷款类型的数量('变量'或'固定')然后将其连接回原始表格。有效地为每个AccountId子查询:
select
child.AccountId,
child.LoanId,
child.ParentId,
rollup.ModifiedLoanType
from Accounts child
join (select
AccountId,
case (count(distinct LoanType)) when 1 then LoanType else 'Mixed' end as ModifiedLoanType from Accounts group by AccountId) rollup
on rollup.AccountId = child.AccountId
group by child.AccountId
order by child.AccountId, child.LoanId
将其视为SQLFiddle。如果它不是您所需要的,那么您可以修补它,和/或我可以帮助您修改它