我有这张桌子
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
我想通过距离 desc 来订购距 getdate 最小距离的用户(无论 Id_Subscription 是多少)。例子:
Position Id_User Distance in day
1 2 7
2 1 2
我试过这样做
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
但这是不正确的。