I will try to make my question clear with a simple eg.
As alias is not valid in the second select statements, what's the way to achieve it?
Edit (from comments)
Suppose i've a table - Students with fields student_id, marks. I want to distribute Rs.500 among students depending on their marks. I want to give the remaining money to the student having max mark (it's done in second select). suppose @max_mark = max mark of the student
A few options here.
use a cte to define
use a table variable
use the definition over and over again
OK now we have some sample data
we want to split $500 across these (according to this function 500*mark/100), with the remainder going to the highest mark
so from this we can see that student 4 gets $250, plus the remaining $25
now I don't actually think that doing this with a UNION ALL is the best way, this should be done all in one query, but this shows you how to do what you where asking in the way you wanted.