select row_number() over (order by Remaindays asc) as _id,
members.Id,
members.Name Name,
DATEDIFF(D,getdate(),DATEADD(D,Duration,StartDay)) Remaindays,
DATEDIFF(D,Birthday,GETDATE())/365.0 as Age,
Birthday,
这是部分代码,提示
消息 207,级别 16,状态 1,第 1 行
列名 'Remaindays' 无效。
如果是表中已有的列就是正常的,算出来的列就是错的,这该怎么解决?
------解决思路----------------------
select row_number() over (order by Remaindays asc) as _id,
members.Id,
members.Name Name,
DATEDIFF(D,getdate(),DATEADD(D,Duration,StartDay)) Remaindays,
DATEDIFF(D,Birthday,GETDATE())/365.0 as Age,
Birthday,
这儿是别名,无法在over直接使用。
------解决思路----------------------
先把查询出来的结果当一个表,然后再外围row_number
select ROW_NUMBER() over(order by a) from
(
select GETDATE() a
)b