Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 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

21 Experts available now in Live!

Get 1:1 Help Now