select * from(select NAME as 姓名,DEPTNAME as 部门,CONVERT(varchar(10) ,ENDSPECDAY, 111 ) as 假期到期时间,LeaveName as 假类,row_number()over(partition by name order by ENDSPECDAY desc ) 到期日期 from USERINFO,DEPARTMENTS,USER_SPEDAY,LeaveClass where (CONVERT(varchar(10),DateAdd(DAY,-1,ENDSPECDAY), 111 )= CONVERT(varchar(10) ,GETDATE(), 111 ) or CONVERT(varchar(10) ,ENDSPECDAY, 111 )=CONVERT(varchar(10) ,GETDATE(), 111 )) and USERINFO.DEFAULTDEPTID=DEPARTMENTS.DEPTID and dbo.USER_SPEDAY.USERID=USERINFO.USERID and LeaveClass.LeaveId=USER_SPEDAY.DATEID)a where a.到期日期=1
这个是SQL2008可以运行的在2000就会报错。因为各种原因只能用SQL2000求各路大神帮忙谢谢
------解决方案--------------------
-- sql2000需要使用自联接或子查询的形式来实现
-- 1 建立临时表,存储好未生成序号前的结果
create table #Origin
(
[姓名] varchar(50)
, [部门] varchar(50)
, [假期到期时间] varchar(10)
, [假类] varchar(50)
)
-- 2 把你句子里的row_number()一列去掉即可,插入到临时表里待用
insert into #Origin ([姓名], [部门], [假期到期时间], [假类])
select NAME as 姓名,DEPTNAME as 部门
,CONVERT(varchar(10) ,ENDSPECDAY, 111 ) as 假期到期时间
,LeaveName as 假类
from USERINFO,DEPARTMENTS,USER_SPEDAY,LeaveClass
where ( CONVERT(varchar(10),DateAdd(DAY,-1,ENDSPECDAY), 111 )= CONVERT(varchar(10) ,GETDATE(), 111 )
or CONVERT(varchar(10) ,ENDSPECDAY, 111 )=CONVERT(varchar(10) ,GETDATE(), 111 )
)
and USERINFO.DEFAULTDEPTID=DEPARTMENTS.DEPTID
and dbo.USER_SPEDAY.USERID=USERINFO.USERID
and LeaveClass.LeaveId=USER_SPEDAY.DATEID
-- 3 这里使用子查询,找出姓名相同的人中,假期到期时间从大到小排序,这里采用count(*)计数的方法,算出T2>=T1的个数,最大的个数当然只有1个,因此就和row_number得到相同结果
select * from
(
select *
, (select count(*) from #Origin T2 where T2.[姓名]=T1.[姓名] and T2.[假期到期时间]>=T1.[假期到期时间]) as [到期日期]
from #Origin T1
)a
where a.到期日期=1