我在嘗試從CASE語句中計算運行總數時遇到了問題。 我有兩個表@report
和@question
和兩個變量@countCurrent
和@countSuggested
。在SQL Server中計算總數
根據@report
表中與靜態值或來自@question
表的值進行比較的數字,我需要增加@countCurrent
或@countSuggested
。
這是我到目前爲止,而不是得到任何一列5的組合,我只得到0/1。我認爲這是JOIN的一部分,但我看不到。
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>[email protected]
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]
在此先感謝!
可能重複的(http://stackoverflow.com/questions/814054/complicated -ql-query-for-a-running-total-column) – JNK
爲什麼你稱它爲總運行?這是一個普通的總數,除非有你或我失蹤的東西。 – Quassnoi
不,你可能是正確的,它不是最好的標題,但幸運的是它已被解決:) –