Solved

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

Posted on 2012-03-20
2
220 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 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