Solved

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

Posted on 2012-03-20
2
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 40

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 40

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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