所以这就是我所拥有的 有一个返回贷款例外的函数。因此,如果有人缺少文件,这是一个例外,或者需要签名,那就是例外等。 问题是此函数返回的所有信息都包含贷款的所有信息。包括金额。 因此,如果单个贷款有6个例外,贷款为1000,那么通过例外总计金额为6000,因为1000存储在每个记录详细信息中。 所以这里有一组我回来的类似记录。
poolDesc| loanNumber| Exception | Amount
Consumer| 123 | Missing Sig| 100
Consumer| 123 | Missing Doc| 100
Consumer| 123 | Late Pymt | 100
Estate | 456 | Address Ent| 2000
Estate | 456 | Missing Doc| 2000
Estate | 789 | Missing Sig| 1000
Consumer| 345 | Missing Sig| 500
我正在寻找的选择是:
POOL CountExceptions LoanAmount
Consumer 4 600
Estate 3 3000
必须有一种方法可以做到这一点,并且如果有帮助,它会进入SSRS报告 感谢
答案 0 :(得分:0)
SELECT poolDesc Pool,
SUM(CountExceptions) CountExceptions,
SUM(LoanAmount) LoanAmount
FROM (
SELECT
poolDesc ,
COUNT(*) CountExceptions,
SUM(Amount) OVER (PARTITION BY poolDesc, loanNumber ) LoanAmount
FROM
loanExceptions
GROUP BY poolDesc, loanNumber, Amount
) a
GROUP BY poolDesc
完整的测试脚本
create table loanExceptions (
poolDesc varchar(50),
loanNumber int,
Exception varchar(50),
Amount float)
insert into loanExceptions values
('Consumer',123,'Missing Sig',100),
('Consumer',123,'Missing Doc',100),
('Consumer',123,'Late Pymt',100),
('Estate',456,'Address Ent',2000),
('Estate',456,'Missing Doc',2000),
('Estate',789,'Missing Sig',1000),
('Consumer',345,'Missing Sig',500)
SELECT poolDesc Pool,
SUM(CountExceptions) CountExceptions,
SUM(LoanAmount) LoanAmount
FROM (
SELECT
poolDesc ,
COUNT(*) CountExceptions,
SUM(Amount) OVER (PARTITION BY poolDesc, loanNumber ) LoanAmount
FROM
loanExceptions
GROUP BY poolDesc, loanNumber, Amount
) a
GROUP BY poolDesc
DROP TABLE loanExceptions