Avatar of tanj1035
tanj1035

asked on 

SQL, How to combine queries by a common column?

Hi Experts,

I have wrote  queries, all of them have a common column ' Store Full Name'. I want to combine all queries to show a result like below. Can you help me on it?  Thank you.

Result.PNG

The Queries needed to be combined are below

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

Open in new window


------# 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

Open in new window


---------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

Open in new window


---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

Open in new window

Microsoft SQL Server

Avatar of undefined
Last Comment
PortletPaul

8/22/2022 - Mon