Home Forums SQL Server 2008 T-SQL (SS2K8) Converting table-value function to set based query RE: Converting table-value function to set based query

  • Thanks for the help. I had to make a minor edit but it works great. I need to sort the groups by the date instead of the identifier since there are times when those numbers get out of order. I tested the change on a much larger data set and the query times went from minutes to seconds. 😀

    Here is the version of the CTE I used.

    WITH Scores AS(

    SELECT EVL.Account,

    EVL.Ident1,

    EVL.EvalDate,

    FIL.Module_ID,

    AVG(CASE WHEN ANSWER_VALUE IN (0,1,2,3) THEN ANSWER_VALUE*1.0 ELSE NULL END)*10.0 AS Score,

    DENSE_RANK() OVER(PARTITION BY EVL.Account ORDER BY EVL.EvalDate DESC) ranking

    FROM #evalanswers AS ANS

    JOIN #filter AS FIL ON ANS.Answer_ID = FIL.Answer_ID

    JOIN #evaluations EVL ON EVL.Ident1 = ANS.Ident1

    WHERE ANS.Answer_Value IN (-1,0,1,2,3)

    AND EVL.COMPLETE = 1

    GROUP BY EVL.Account,

    EVL.Ident1,

    EVL.EvalDate,

    FIL.Module_ID

    )