?
Solved

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

Posted on 2012-03-20
2
Medium Priority
?
254 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 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 2000 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

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

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

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 …
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

755 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