• SQL_Surfer (7/29/2013)


    Where are you doing SELECT @Var5 = max(mycol) FROM Mytable WHERE mycol = @a ? I need to get max on that column from Mytable.

    You can't get MAX(mycol) FROM Mytable when you have the filter mycol = @a.

    MAX(mycol) can only be equal to @a, so @Var5 = @a and Val5 = m.mycol.

    Unless of course your intention is to aggregate Mytable to one row. If it is, then adjust my query for this.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden