I have this table
Id_User Id_Subscription Date_Expiration
1 1 2013-01-21
1 2 2013-01-28
2 1 2013-01-15
2 2 2013-01-30
2 3 2013-01-31
I want to order users with min distance from getdate (regardless of Id_Subscription) order by distance desc. Example:
Position Id_User Distance in day
1 2 7
2 1 2
I have tried to do that
SELECT ROW_NUMBER() OVER(ORDER BY COALESCE(MIN(us.Date_Expiration), CAST('2015-01-29 16:30:23.000' AS DATE)) DESC, us.id_user) AS Row, us.id_user FROM User_Subscription us GROUP BY a.Id_Anagrafica
but it's not correct.