This SQL query executes an analysis on a table, likely...
This SQL query executes an analysis on a table, likely related to financial or loan data, and creates a unified result set by combining the results from three different conditions (BK
, DQ
, Repo
). Here's a breakdown of what the code does:
1. General Structure
- The query uses three
SELECT
statements combined withUNION
. - Each
SELECT
calculates aggregations and computes derived statistics. - Conditions (
where BK = 'Yes'
,where DAYSDEL > 30
, andwhere cRecoveryStatus <> '0'
) filter the data in each part of theUNION
. - Finally, the result set is ordered in descending order of the total principal balance (
sum(Prinbal)
).
2. Columns Computed
Each SELECT
statement computes the following columns:
-
Identifier (
'BK'
,'DQ'
,'Repo'
):- A label corresponding to each part of the union.
-
CountID:
- The count of rows (
count(*)
) in the filtered dataset for the specific condition.
- The count of rows (
-
Balance:
- Sum of
Prinbal
(which likely stands for "principal balance").
- Sum of
-
FICO:
- A weighted average of FICO scores for loans where FICO > 0, weighted by
Prinbal
.
- A weighted average of FICO scores for loans where FICO > 0, weighted by
-
APR:
- Average interest rate, computed as the sum of principal-weighted
CURRRATE
divided by the sum ofPrinbal
.
- Average interest rate, computed as the sum of principal-weighted
-
[Zero APR]:
- The proportion of loans with
CURRRATE = 0
compared to the totalPrinbal
.
- The proportion of loans with
-
LTV:
- Loan-to-value ratio, scaled by multiplying by 100.
-
LTV Brackets:
- Proportions of loans falling into different LTV brackets:
[LTV<100]
: Loans with100 * LTV < 100
.[LTV 100 to 120]
: Loans with100 * LTV
between100
and120
.[LTV>120]
: Loans with100 * LTV > 120
.
- Proportions of loans falling into different LTV brackets:
-
[Original Term] & [Remaining Term]:
- Weighted averages (based on
Prinbal
) of the loan's original term (ORIGTERM
) and remaining term (REMTERM
), respectively.
- Weighted averages (based on
-
[Term 84]:
- Proportion of loans with an original term of exactly
84
.
- Proportion of loans with an original term of exactly
-
New:
- Proportion of loans designated as "new" (
NewUsed = 'New'
).
- Proportion of loans designated as "new" (
-
DQ:
- Proportion of delinquent loans (
DAYSDEL > 30
).
- Proportion of delinquent loans (
-
CNL (Cumulative Net Loss):
- A calculation for cumulative net loss, computed as
-sum(PM41_Net_Loss_Node_adj) / sum(AMTFIN)
.
- A calculation for cumulative net loss, computed as
3. Filters in Each Part of the UNION
-
First Block (
BK
):- Filters where
BK
equals'Yes'
.
- Filters where
-
Second Block (
DQ
):- Filters where
DAYSDEL > 30
(indicating delinquent loans).
- Filters where
-
Third Block (
Repo
):- Filters where
cRecoveryStatus <> '0'
(likely repossessed loans).
- Filters where
4. Final Output
- Combines the aggregated results from the three conditions (
BK
,DQ
,Repo
) into a single result set. - Orders the rows based on
sum(Prinbal)
(total principal balance) in descending order.
5. Summary
The code aggregates loan or financial data into several key metrics for three distinct data subsets (e.g., 'Bankrupt' (BK
), 'Delinquent' (DQ
), and 'Repossession' (Repo
)). Metrics include count of loans, total/principal balances, averages, proportions, and weighted averages for various financial attributes such as FICO scores, APR, loan-to-value ratios, terms, and delinquency. The results are displayed in a unified table ordered by the overall balance for easier comparative analysis.