Het lijkt erop dat de snelste manier om te doen wat u wilt, is om uw counted2
. te wijzigen CTE, dus de kolom TimesTested
houd rekening met je logica. Dus het zou moeten zijn:
counted2 as (
SELECT
client_id,
CASE WHEN TimesTested >= 12 THEN 12 ELSE TimesTested END TimesTested,
CAST(COUNT(*) AS varchar(30)) AS count,
CAST(AVG(testfreq) as varchar(30)) as TestFreq,
CAST(STDEV(TestFreq) as varchar(30)) Stdv
FROM counted
GROUP BY
client_id,
CASE WHEN TimesTested >= 12 THEN 12 ELSE TimesTested END
)