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.
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