I have the following code in which I need to check something in the ELSE. The problem is how to form it correctly. I could use a cursor I guess to transverse through the records.
In the Else, I want to:
1) Check the count of the results of my statement. IF > 1 then check to see if m.original is between lowlimit and highlimit for any of those records found
2) If m.original is between the lowlimit and highlimit, then select Fee1 from FeeScheduleDetails
m.original and m.FeeSchedule is out here somewhere, just know this...it's part of the query that you don't see
CASE WHEN Len(c.FeeSchedule) < 3 then
CONVERT(int, c.feeSchedule)
ELSE
Select Count(*) FROM FeeScheduleDetails fd
INNER JOIN Master m ON m.FeeSchedule = fd.code
IF Count(*) > 3
Check to see if m.original is BETWEEN fd.lowlimit AND fd.highlimit
If yes, then bring me back fee1, if no then just bring me back m.FeeSchedule
END AS FeeSchedule
Master
---------
FeeSchedule
FeeScheduleDetails
----------------------
Code
LowLimit
HighLimit
Fee1
In the Else, I want to:
1) Check the count of the results of my statement. IF > 1 then check to see if m.original is between lowlimit and highlimit for any of those records found
2) If m.original is between the lowlimit and highlimit, then select Fee1 from FeeScheduleDetails
m.original and m.FeeSchedule is out here somewhere, just know this...it's part of the query that you don't see
CASE WHEN Len(c.FeeSchedule) < 3 then
CONVERT(int, c.feeSchedule)
ELSE
Select Count(*) FROM FeeScheduleDetails fd
INNER JOIN Master m ON m.FeeSchedule = fd.code
IF Count(*) > 3
Check to see if m.original is BETWEEN fd.lowlimit AND fd.highlimit
If yes, then bring me back fee1, if no then just bring me back m.FeeSchedule
END AS FeeSchedule
Master
---------
FeeSchedule
FeeScheduleDetails
----------------------
Code
LowLimit
HighLimit
Fee1

Help with Case Statement
Adam Miles
Amadeus
--
Hugo Kornelis, SQL Server MVP
adshah
. Sometimes I don't have all those statements created because the statement I'm working on is it and is my only attempt/approach at the time! Yes, I could have posted some data examples though. Thanks for your explanation.
Oliver-LundK.de
thanks for the heads up. Here's the entire query...and a new attept at fee1
INSERT INTO ReportingServer.dbo.DCR
SELECT
m.customer,
c.name,
c.customer,
c.state,
CASE WHEN Len(c.FeeSchedule) < 3 THEN
CONVERT(int, c.feeSchedule)
WHEN Len(c.FeeSchedule) > 3 THEN
SELECT fd.Fee1 FROM FeeScheduleDetails fd
where c.feeSchedule = fd.code AND m.original BETWEEN fd.LowLimit AND fd.HighLimit
ELSE
CONVERT(int, c.feeSchedule)
END AS FeeSchedule,
m.Branch,
CASE WHEN ph.batchtype = 'PUR' OR ph.batchtype = 'PAR' OR ph.batchtype = 'PCR' Then
(ph.totalpaid - ph.ForwardeeFee)
ELSE
0.00
END AS [Posted Amount],
ph.systemmonth,
ph.datepaid,
ph.totalpaid,
ph.batchtype,
m.desk,
0 AS [New Old CC],
0 AS [New Old PDC],
'In-House' AS Type,
1 AS Active,
ph.UID,
m.number,
dc.amount CC,
p.amount AS PDC,
m.original,
CONVERT(money, ph.OverPaidAmt),
0,
0,
''
FROM dbo.Master m (NOLOCK) LEFT JOIN dbo.payhistory ph ON m.number = ph.number
LEFT JOIN dbo.DebtorCreditCards dc ON dc.number = m.number
LEFT JOIN dbo.pdc p ON p.number = m.number
LEFT JOIN dbo.Customer c ON c.Customer = m.Customer
LEFT JOIN Apex_ReportingServer.dbo.FeeGoal fg ON fg.CustomerID = c.Customer
GROUP BY m.customer,
c.name,
c.customer,
c.state,
c.FeeSchedule,
m.Branch,
ph.OverPaidAmt,
ph.systemmonth,
ph.datepaid,
ph.totalpaid,
ph.batchtype,
m.desk,
ph.UID,
m.number,
dc.amount,
p.amount,
m.original ,
ph.systemmonth,
ph.systemyear,
ph.ForwardeeFee
HAVING ph.systemmonth = datepart(mm, getdate()) AND ph.batchtype <> 'DA' AND
ph.batchtype <> 'DAR' AND ph.systemyear = datepart(yy, getdate())
AND (c.Name is not null AND c.Name <> '')
ORDER BY m.customer
Markus W&#246;&#223;
I am not sure what you are asking. I have also never seen a CASE statement used in this way. CASE statements are always used within a SELECT clause to select a result value to display in the column based on the value in a particular row. SQL has an IF condition of form:
IF.....
BEGIN
END
ELSE
BEGIN
END
Firstly I suggest you use IF that instead. Is your IF statement returning multiple values, or just one value If it is just returning one value (and with a count(*) you normally only return one valuer, unless using a GROUP BY) then use and IF, otherwise use a case statement. IF example below)
declare @result int
declare @lowlimit int
declare @highlimit int
IF Len(c.FeeSchedule) < 3
BEGIN
set @result = CONVERT(int, c.feeSchedule)
END
ELSE
work out @lowlimit and @highlimit here.....
delcare @cnt int
Select @cnt = Count(*) FROM FeeScheduleDetails fd
INNER JOIN Master m ON m.FeeSchedule = fd.code
IF @cntBETWEEN @lowlimit AND @highlimit
BEGIN
@result = (get Fee1......)
END
ELSE
BEGIN
@result = (get m.FeeSchedule.....)
END
END
Otherwise, if you want the second conditional to be a CASE statement, you will need to rewrite it accordingly
Clarity Consulting
Danielsan Ichiban
--
Hugo Kornelis, SQL Server MVP
Rob Goodridge
DeCion05
--
Hugo Kornelis, SQL Server MVP
MGraves