Link to home
Start Free TrialLog in
Avatar of tanj1035
tanj1035

asked on

SQL, how to pass multiple values in a single parameter

Hi Experts,
@State has multiple values. In SSRS, the user has ability to select null value or multiple values in @State. How can I modify the query? Thank you.




CREATE procedure [dbo].[sp_rept_Online_QCL_StaticPoolWithLates]

 @EndDate datetime,
 @State char(2)



/******************************************************************
Created by:		
Created on:		4/22/2016
Description:	For the report   QCL Static Pool With Lates 
*******************************************************************/

AS
BEGIN



Select 
	LoanMonth,
	count(LoanMonth) as OriginatedNumber,
	SUM(LoanAmountFinanced) as OriginatedAmt,
	SUM(LoanAmountFinanced*totalPaymentsRequired)/SUM(LoanAmountFinanced) as WeightedTerms,
	SUM(LoanAmountFinanced*LoanAPR)/SUM(LoanAmountFinanced) as WeightedAPR,
	Sum(LoanAmountFinanced*CreditBeacon)/SUM(LoanAmountFinanced) as WeightedBeacon,
	---OPEN LOANS---
	SUM(Case When LoanStatusID in (1,7) Then 1 Else 0 END) as [ActiveNumber],
	SUM(Case When LoanStatusID in (1,7) THEN OPB Else 0 END) as CurrentOPB,
	SUM(Case When LoanStatusID in (1,7) THEN LoanAmountFinanced*LoanAPR Else 0 END)/SUM(Case When LoanStatusID in (1,7) THEN LoanAmountFinanced  END) as WeightedOpenAPR,
	SUM(Case When LoanStatusID in (1,7) THEN LoanAmountFinanced*CreditBeacon Else 0 END)/SUM(Case When LoanStatusID in (1,7) THEN LoanAmountFinanced  END) as WeightedOpenBeacon,
	---CLOSED LOANS---
	SUM(Case When LoanStatusID = 2 Then 1 Else 0 END) as [ClosedNumber],
	SUM(Case When LoanStatusID = 2 THEN LoanAmountFinanced Else 0 END) as [ClosedOriginatedAmt],
	SUM(Case When LoanStatusID = 2 THEN LoanAmountFinanced*LoanAPR Else 0 END)/SUM(Case When LoanStatusID =2 THEN LoanAmountFinanced  END) as WeightedClosedAPR,
	SUM(Case When LoanStatusID = 2 THEN LoanAmountFinanced*CreditBeacon Else 0 END)/SUM(Case When LoanStatusID =2 THEN LoanAmountFinanced  END) as WeightedClosedBeacon,
	---CHARGED OFF LOANS---
	SUM(Case When LoanStatusID in (5,6) OR (LoanStatusID not in (2,8) and  DaysLate>30) Then 1 Else 0 END) as [ChargeOffNumber],
	SUM(Case When LoanStatusID in (5,6) OR (LoanStatusID not in (2,8) and  DaysLate>30) THEN OPB Else 0 END) as [ChargeOffOriginatedAmt],
	SUM(Case When LoanStatusID in (5,6) OR (LoanStatusID not in (2,8) and  DaysLate>30) THEN LoanAmountFinanced*LoanAPR Else 0 END)/SUM(Case When LoanStatusID in (5,6) OR (LoanStatusID not in (2,8) and  DaysLate>30) THEN LoanAmountFinanced  END) as WeightedChargeOffAPR,
	SUM(Case When LoanStatusID in (5,6) OR (LoanStatusID not in (2,8) and  DaysLate>30) THEN LoanAmountFinanced*CreditBeacon Else 0 END)/SUM(Case When LoanStatusID in (5,6) OR (LoanStatusID not in (2,8) and  DaysLate>30) THEN LoanAmountFinanced  END) as WeightedChargeOffBeacon
	

	

From
(Select
	LoanDate,
	DateAdd(d,-Day(LoanDate)+1,convert(char(10),LoanDate,101)) as LoanMonth,
	totalPaymentsRequired,
	LoanAmountFinanced,
	LoanAPR,
	(Select top 1 StatusTransactionTypeID from tblStatusTransactions Where StatusTransactionLoanID=tblLoans.LoanID and StatusTransactionDate <=@EndDate Order by StatusTransactionDate desc)
	LoanStatusID,
	Case When CreditBeacon<100 Then null else CreditBeacon END as CreditBeacon,
	(Select Top 1 OpenPrincipleBalance from tblReportingTransactions Where LoanID=tblLoans.LoanID Order by ReportingTransactionID Desc) as OPB,
	(Select Top 1 DaysLate from tblReportingTransactions Where LoanID=tblLoans.LoanID Order by ReportingTransactionID Desc ) as DaysLate
From
	tblLoans
left join tblCreditApps
on tblCreditApps.CreditCustomerID=tblLoans.CustomerID
inner join tblWebStores  
on tblLoans.LoanStoreID=tblWebStores.LoanStoreID
Where WebStoreState in (@State) and LoanStatusID<>8 and LoanDate<=@EndDate and tblLoans.LoanStoreID between 800 and 849 )tblOut
Group by LoanMonth
Order by LoanMonth

END

GO

Open in new window

Avatar of Mlanda T
Mlanda T
Flag of South Africa image

What does @State look like when multiple values are selected?
For multiple values, assuming perhaps comma seperated, increase the size of char(2) because that is only 2 characters. The right size will depend on what the values of @State are and how many user could possibly select.
Avatar of tanj1035
tanj1035

ASKER

In SSRS, there is a dataset for @State. It returned the result below. In the @State parameter setting, it allowed null value and multiple values. So, the user can select multiple values or null in the selection list. Thank you.


AL
CA
DE
GA
ID
IL
MO
MT
NH
NV
OR
SD
UT
WI
SOLUTION
Avatar of Mlanda T
Mlanda T
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you guys for your detailed solution.
tanj1035, if your issue has been solved please choose one or more comments as solution.
Cheers.
Sorry for the delay. I did not realize that I have not assigned points and have closed the questions successfully. Thank you, guys.