问题描述--查询某个学生在校期间系的所用名及使用该名的起止日期。
答:首先设计
  学生表Student(*学号 id,系编号 n_o, 名称版本号 nu, 入校时间 cometime ,离校时间 lefttime)
  系表 Department(*系编号 n_o)
  系名表Depart(*系编号 n_o,*名称版本号 nu,名称设定日期 Settime,系名称 Dname)
注:名称版本号码是流水号,下一个流水号对应的设定日期就是上一个名称使用的截止日期现在就是有一个问题,查询名称变更信息的起始日期时,怎样根据学生的学号来查询当时使用系名使用的终止日期,
  select d.n_o ,d.Settime 开始使用时间 from Student s join Depart d on s.n_o=d.n_o and s.nu=d.nu 
    where s.id=已知学生的学号
       对于终止日期,就是Depart中该名称版本号记录的下一条记录的设定日期,这样在同一个select语句中实现?高手指点下,谢谢 

解决方案 »

  1.   

    ;with t as(select idd=row_number()over(order by getdate()),d.n_o,Settime
    from Student s join Depart d on s.n_o=d.n_o and s.nu=d.nu where s.id=已知学生的学号)
    select a.n_o ,a.Settime 开始使用时间,b.Settime 终止日期 from t a,t b on a.idd=b.idd-1
      

  2.   

    select m.* , 截止日期 = (select min(Settime) from Depart n where n.n_o = m.n_o) from Student mselect m.* , 截止日期 = (select top 1 Settime from Depart n where n.n_o = m.n_o and n.nu > m.nu order by Settime) from Student mselect m.* , 截止日期 = (select min(Settime) from Depart n where n.n_o = m.n_o) from Student m where m.id = '...'select m.* , 截止日期 = (select top 1 Settime from Depart n where n.n_o = m.n_o and n.nu > m.nu order by Settime) from Student m where m.id = '...'
      

  3.   

    --如果查询到无截止日期,即最大流水号,则显示为'暂无截止日期'
    select m.* , 截止日期 = isnull((select min(convert(varchar(10),Settime,120)) from Depart n where n.n_o = m.n_o),'暂无截止日期') from Student mselect m.* , 截止日期 = isnull((select top 1 convert(varchar(10),Settime,120) from Depart n where n.n_o = m.n_o and n.nu > m.nu order by Settime),'暂无截止日期') from Student mselect m.* , 截止日期 = isnull((select min(convert(varchar(10),Settime,120)) from Depart n where n.n_o = m.n_o),'暂无截止日期') from Student m where m.id = '...'select m.* , 截止日期 = isnull((select top 1 convert(varchar(10),Settime,120) from Depart n where n.n_o = m.n_o and n.nu > m.nu order by Settime),'暂无截止日期') from Student m where m.id = '...'