asked on
select st.StoreID, StoreName,
CASE WHEN storefullname like '%Anderson Financial Services%' THEN 'Anderson Financial Services, LLC'
WHEN storefullname like '%Kipling Financial Services%' THEN 'Kilpling Financial Services, LLC'
WHEN storefullname like '%LoanSmart%' THEN 'LoanSmart, LLC'
END AS StoreFullName
Into #GroupByStores
from tblStores st join tblStores_tblStoreTypes sst on st.StoreID=sst.storeid
where st.StoreState ='VA'
AND sst.StoreTypeID=1
AND (StoreFullName LIKE '%Anderson Financial Services%' OR StoreFullName LIKE '%Kipling Financial Services%' OR StoreFullName LIKE '%LoanSmart%')
select storefullname
,COUNT(loanid) as numberofloans ---verified 54486----
,SUM(loanamountfinanced) as dolloaramountofloans
,MAX(LoanAmountFinanced) as highestamountofloans
,MIN(LoanAmountFinanced) as lowestamountofloans
,AVG(Loanamountfinanced) as avgamountofloans
,MAX(FN.TILAAPR) as highestAPR
,MIN(FN.TILAAPR) as lowestAPR
,AVG(FN.TILAAPR) as avgAPR
from
(Select S.storefullname
, L.loanid
, L.loanamountfinanced
, FN.TILAAPR
From tblLoans L inner join #GroupByStores S on L.LoanStoreID =S.STOREID
CROSS APPLY GetTILAInfo(LoanID) as FN
Where L.LoanDate >='20150101' and LoanDate <= '20151231'
AND L.AssetTypeID=1) a
Group by storefullname
------# of customers---
SELECT
s.Storefullname
,count(distinct c.customerssn) as numberofcustomers
FROM
tblCustomers c left join
tblLoans L on L. CustomerID = c.customerID INNER JOIN
#GroupByStores S on L.LoanStoreID =S.STOREID
Where L.LoanDate >='20150101' and LoanDate <= '20151231'
GROUP BY STOREFULLNAME
---------get # of late payment ---
Select
S.storefullname
,COUNT (DISTINCT L.LOANId) AS numberoflatepayment
From tblLoans L inner join
tblReportingTransactions R on L.loanID = R. LoanID INNER JOIN
#GroupByStores S on L.LoanStoreID =S.STOREID
WHERE R.TransactionDate >='20150101' and R.TransactionDate <='20151231'
AND R.DaysLate>=60
AND L.LoanDate >= '20101001'
Group by S.storefullname
---repossessedCars & Sold Cars-----
select
s.storefullname
, COUNT(L.loanid ) as repossessed
, Sum(case when L.AuctionCheckDate >= '20150101' AND L.AUCTIONCHECKDATE <= '20151231'
AND L.AuctionCheckAmount >0 then 1 else 0 end ) as soldcars
FROM tblStatusTransactions ST INNER JOIN
tblLoans L ON ST.StatusTransactionLoanID=L.LoanID INNER JOIN
#GroupByStores S ON S.STOREID=L.LOANSTOREID
WHERE ST.StatusTransactiontypeID =3
AND L.LoanDate >= '20101001'
AND ST.StatusTransactionDate >='20150101' AND ST.StatusTransactionDate <= '20151231'
GROUP BY S.STOREFULLNAME