with userlist as (
select u.usern, u.user_id,, u.state, p1sex, p1orient, p2sex, p2orient, couple, p1age, p2age, 
dbo.getdistance(uz.lattitude, uz.longitude, mz.lattitude, mz.longitude) as distance, 
isnull((select image_url from users_images where u.user_id = users_images.image_owner and image_default =1),'no_profile.jpg') as image_path, 
row_number() over (order by distance, usern) as RowNum 
from users u, zips uz left join zips mz on mz.zip_code = (select zip from users where user_id =10) 
where u.confirmed = 1 and u.ban = 0 and uz.zip_code = 
) select * from userlist where RowNum between 1 and 12
I want to use that query, but distance is a function in that database and it doesnt work like this. i get Invalid column name 'distance'. I am sure I am missing something simple but I cannot find it yet.

it worked before adding the row_number and if it is removed from that order by it works