Solved

Can I simplify this query using views, stored procedures, etc

Posted on 2012-03-20
2
210 Views
Last Modified: 2012-08-13
I am wondering if I am going about this the wrong way. For example, if you look at the reason for denials, is there an easy way to create a view or something that I can refer to rather than using the code:

and (reason_for_denial not in('auto withdrawl during esign !','Auto Withdrawn During LatitudePayment Return !','Alternate Product ILP Accepted','Fraud','DNQ - Direct Deposit','DNQ - No Longer Employed','DNQ - Income','DNQ - Card Account','DNQ - Work Number',
declare @fiscalyear as int
declare @fiscalmonth as int
set @fiscalyear = '2012'
set @fiscalmonth = '201203'

select *
from

(
select DATE, 
SUM(case when loan_type = 'pdl' and app_type = 'std' and cust_type = 'new' then 1 else null end) as 'PDL New Apps',
SUM(case when loan_type = 'pdl' and app_type = 'std' and cust_type in ('rtn','ret','ina_rtn','pod_rtn') then 1 else null end) as 'PDL Rtn Apps',
SUM(case when loan_type = 'pdl' and app_type = 'ren' then 1 else null end) as 'PDL Ren Apps',
SUM(case when loan_type = 'pdl' and cust_type is null then 1 else null end) as 'PDL State Denials',
count(distinct case when loan_type = 'pdl' and app_type = 'std' and cust_type = 'new' and processing_status <> 'denied'
	and (reason_for_denial not in('auto withdrawl during esign !','Auto Withdrawn During LatitudePayment Return !','Alternate Product ILP Accepted'
	,'Fraud','DNQ - Direct Deposit','DNQ - No Longer Employed','DNQ - Income','DNQ - Card Account','DNQ - Work Number',
	'DNQ - Home/Cell Number','Store Collections','Auto Withdrawn During ACH Return !') or (reason_for_denial is NULL)) 
	then bo_code else null end) as 'PDL New Approved',
	count(distinct case when loan_type = 'pdl' and app_type = 'std' and cust_type in ('rtn','ret','ina_rtn','pod_rtn') and processing_status <> 'denied'
	and (reason_for_denial not in('auto withdrawl during esign !','Auto Withdrawn During LatitudePayment Return !','Alternate Product ILP Accepted'
	,'Fraud','DNQ - Direct Deposit','DNQ - No Longer Employed','DNQ - Income','DNQ - Card Account','DNQ - Work Number',
	'DNQ - Home/Cell Number','Store Collections','Auto Withdrawn During ACH Return !') or (reason_for_denial is NULL)) 
	then bo_code else null end) as 'PDL Rtn Approved',
count(distinct case when loan_type = 'pdl' and app_type = 'std' and cust_type = 'new' and processing_status = 'withdrawn' then bo_code else null end) as 'PDL New Withdraws',
count(distinct case when loan_type = 'pdl' and app_type = 'std' and cust_type in ('rtn','ret','ina_rtn','pod_rtn') and processing_status = 'withdrawn' then bo_code else null end) as 'PDL Rtn Withdraws'
from USOnlineReporting.dbo.BO_IN_APP_QUEUE bia
inner join USOnlineReporting.dbo.Business_Dates_All bda on bda.Date = dateadd(dd,0,datediff(dd,0,app_date))
where FiscalYear = @fiscalyear
and FiscalMonth = @fiscalmonth
group by date
)a

left join

(
select a.date, 
[PDL New Trans],
[PDL Rtn Trans],
[PDL Ren Trans],
isnull([PDL New Fees],0) - isnull([PDL New Rebate],0) as 'PDL New Fee', 
isnull([PDL Rtn Fees],0) - isnull([PDL Rtn Rebate],0) as 'PDL Rtn Fee',
isnull([PDL Ren Fees],0) - isnull([PDL Ren Rebate],0) as 'PDL Ren Fee'
from 

	(
	select date
	from usonlinereporting.dbo.business_dates_all bda
	where FiscalYear = @fiscalyear
	and FiscalMonth = @fiscalmonth
	)a
	
	left join
	(
	select date,
	count(case when app_type = 'std' and cust_type = 'new' and acttype = 'adv' and apportion_id <> 'nf' and fee <> '0' and loan_status_id <> 'v' then fee else null end) as 'PDL New Trans',
	count(case when app_type = 'std' and cust_type in ('rtn','ret','ina_rtn','pod_rtn') and acttype = 'adv' and apportion_id <> 'nf' and fee <> '0' and loan_status_id <> 'v' then fee else null end) as 'PDL Rtn Trans',
	count(case when app_type = 'ren' and acttype = 'agn' and apportion_id <> 'nf' and fee <> '0' and loan_status_id <> 'v' then fee else null end) as 'PDL Ren Trans',
	sum(case when app_type = 'std' and cust_type = 'new' and acttype = 'adv' and apportion_id <> 'nf' and fee <> '0' then fee else null end) as 'PDL New Fees',
	sum(case when app_type = 'std' and cust_type in ('rtn','ret','ina_rtn','pod_rtn') and acttype = 'adv' and apportion_id <> 'nf' and fee <> '0' then fee else null end) as 'PDL Rtn Fees',
	sum(case when app_type = 'ren' and acttype = 'agn' and apportion_id <> 'nf' and fee <> '0' then fee else null end) as 'PDL Ren Fees'
	from usonlinereporting.dbo.stars_trans st
	inner join usonlinereporting.dbo.bo_in_app_queue bia on bia.app_no = st.loan_code
	inner join USOnlineReporting.dbo.Business_Dates_All bda on bda.Date = st.tran_date
	where FiscalYear = @fiscalyear
	and FiscalMonth = @fiscalmonth
	group by date
	)b
	
	on b.date = a.date
	
	left join
	(
	select date,
	sum(case when app_type = 'std' and cust_type = 'new' and rebate_amt <> '0' then rebate_amt else null end) as 'PDL New Rebate',
		sum(case when app_type = 'std' and cust_type in ('rtn','ret','ina_rtn','pod_rtn') and rebate_amt <> '0' then rebate_amt else null end) as 'PDL Rtn Rebate',
			sum(case when app_type = 'ren' and rebate_amt <> '0' then rebate_amt else null end) as 'PDL Ren Rebate'
	from usonlinereporting.dbo.st_lo_master slm
	inner join usonlinereporting.dbo.bo_in_app_queue bia on bia.app_no = slm.loan_code
	inner join USOnlineReporting.dbo.Business_Dates_All bda on bda.Date = dateadd(dd,0,datediff(dd,0,loan_end_date))
	where FiscalYear = @fiscalyear
	and FiscalMonth = @fiscalmonth
	group by date
	)c
	
	on c.date = a.date

)b

on b.date = a.date
order by a.date

Open in new window

'DNQ - Home/Cell Number','Store Collections','Auto Withdrawn During ACH Return !') or (reason_for_denial is NULL))
0
Comment
Question by:dplowman
  • 2
2 Comments
 
LVL 39

Expert Comment

by:lcohan
ID: 37744392
The best in my opinion would be to use CTE in SQL 2008 and check the query execution plan as well to see if you have any missing indexes.
0
 
LVL 39

Accepted Solution

by:
lcohan earned 500 total points
ID: 37744417
And yes you could put that in a view like below but the best would be to add a lookup table and populate it with those values then you could add more as needed:

create view v_list as
select null
union all
select 'auto withdrawl during esign !'
union all
select 'Auto Withdrawn During LatitudePayment Return !'
union all
select 'Alternate Product ILP Accepted'
union all
select 'Fraud'
union all
select 'DNQ - Direct Deposit'
union all
select 'DNQ - No Longer Employed'
union all
select 'DNQ - Income'
union all
select 'DNQ - Card Account'
union all
select 'DNQ - Work Number'
union all
select 'DNQ - Home/Cell Number'
union all
select 'Store Collections'
union all
select 'Auto Withdrawn During ACH Return !'
      as reason_for_denial

GO
select * from v_list
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now