I’m having trouble trying to calculate a running total from within a CASE statement.
I have two tables @report and @question and two variables @countCurrent and @countSuggested.
Based off the numbers in the @report table compared against either a static value or a value from the @question table I need to increment either @countCurrent or @countSuggested.
Here is what I have so far but instead of getting some combination of 5 in either column, I am only getting 0/1. I think it is part of the JOIN but I can’t see what.
declare @MinSuccessRate float,
@countCurrent int,
@countSuggested int
declare @report table
(
intID int identity(1,1),
intReportID int,
intParticipantID int,
acceptable float,
optimum float
)
insert @report
select 1,1,.25,.75 union all
select 1,2,.45,.75 union all
select 1,3,.35,.75 union all
select 1,4,.55,.75 union all
select 1,5,.65,.75
declare @question table
(
intID int identity(1,1),
intParticipantID int,
answer float
)
insert @question
select 1,35 union all
select 1,55 union all
select 1,65 union all
select 1,75 union all
select 1,85
SET @MinSuccessRate=0.75
SET @countCurrent=0
SET @countSuggested=0
UPDATE @report
SET @countCurrent=
CASE WHEN acceptable>=@MinSuccessRate
THEN @countCurrent+1
ELSE 0
END,
@countSuggested=
CASE WHEN optimum*100 >=q.answer
THEN @countSuggested+1
ELSE 0
END
FROM @report pr
INNER JOIN @question q
ON pr.intParticipantID=q.intParticipantID
WHERE pr.intReportID=1
select @countCurrent [Current],@countSuggested [Suggested]
Thanks in advance!
In a multiple table
UPDATE, each target record can be updated at most once (regardless of how many times is it returned by the join).However, you don’t need
UPDATEhere at all: