SELECT
COUNT(L.LOANID) AS numberofloans
,SUM (case when ST.StatusTransactionTypeID=5 then 1 else 0 end ) as numberofbankruptcyloans
,SUM (case when ST.StatusTransactionTypeID=6 then 1 else 0 end ) as numberofwriteoffloans
, (
SELECT COUNT(LOANID)as bankruptcyloanswithnopayment
FROM (
SELECT MAX(STATUSTRANSACTIONID) AS ID,STATUSTRANSACTIONLOANID
FROM tblStatusTransactions
GROUP BY StatusTransactionLoanID
)MAXID INNER JOIN
tblStatusTransactions ST ON MAXID.ID = ST.StatusTransactionID INNER JOIN
#LOANID L ON L.LOANID=MAXID.StatusTransactionLoanID
WHERE STATUSTRANSACTIONTYPEID=5
AND LOANID NOT IN (SELECT R.LOANID FROM tblReportingTransactions R
WHERE R.TransactionTypeID IN (1,2,3,11,23))
) as bankruptcyloanswithnopayment
,(SELECT
COUNT (LOANMAXDATEID.LOANID) AS NUMBEROFLATEPAYMENT
FROM
(
SELECT MAX(REPORTINGTRANSACTIONID) AS MAXDATEID
,OPENLOANS.LOANID
FROM
(
SELECT ST.StatusTransactionLoanID AS LOANID
FROM
(
SELECT MAX(STATUSTRANSACTIONID) AS ID,STATUSTRANSACTIONLOANID
FROM tblStatusTransactions
GROUP BY StatusTransactionLoanID
)MAXID INNER JOIN
tblStatusTransactions ST ON MAXID.ID = ST.StatusTransactionID INNER JOIN
#LOANID L ON L.LOANID=MAXID.StatusTransactionLoanID
WHERE StatusTransactionTypeID IN (1,7)
)OPENLOANS INNER JOIN
tblReportingTransactions RT ON RT.LoanID = OPENLOANS.LOANID
Group by OPENLOANS.LoanID
) LOANMAXDATEID LEFT JOIN
tblReportingTransactions RT ON RT.ReportingTransactionID =LOANMAXDATEID.MAXDATEID
WHERE DATEDIFF (d,nextduedate, GETDATE()) >=30
) as numberoflatepayment
FROM
(
SELECT MAX(STATUSTRANSACTIONID) AS ID,STATUSTRANSACTIONLOANID
FROM tblStatusTransactions
GROUP BY StatusTransactionLoanID
)MAXID INNER JOIN
tblStatusTransactions ST ON MAXID.ID = ST.StatusTransactionID INNER JOIN
#LOANID L ON L.LOANID=MAXID.StatusTransactionLoanID
, (
SELECT COUNT(LOANID)as bankruptcyloanswithnopayment
FROM (
SELECT MAX(STATUSTRANSACTIONID) AS ID,STATUSTRANSACTIONLOANID
FROM tblStatusTransactions
GROUP BY StatusTransactionLoanID
)MAXID INNER JOIN
tblStatusTransactions ST ON MAXID.ID = ST.StatusTransactionID INNER JOIN
#LOANID L ON L.LOANID=MAXID.StatusTransactionLoanID
WHERE STATUSTRANSACTIONTYPEID=5
AND LOANID NOT IN (SELECT R.LOANID FROM tblReportingTransactions R
WHERE R.TransactionTypeID IN (1,2,3,11,23))
) as bankruptcyloanswithnopayment
Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.
”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.
Our community of experts have been thoroughly vetted for their expertise and industry experience.
The Most Valuable Expert award recognizes technology experts who passionately share their knowledge with the community, demonstrate the core values of this platform, and go the extra mile in all aspects of their contributions. This award is based off of nominations by EE users and experts. Multiple MVEs may be awarded each year.
This award recognizes a member of Experts Exchange who has made outstanding contributions to the community within their first year as an expert. The Rookie of the Year is awarded to a new expert who has the highest number of quality contributions.