有一张表Student,列包括Name,Sex,Age,Time
Name      Sex       Age      Time
-------------------------------------
张三        男         20       2008-01
李四        女         30       2008-01
王五        男         40       2008-02
王五        女         20       2008-03当姓名有重复时候,希望能在姓名前面加一个id列,比如有2个重名的,第1个重名的id为1,第2个重名的id为2,这2个按照录入时间排序,其他不重名的id都为1,欲实现的效果如下图
通过写Sql语句或者视图怎么实现呢id       Name      Sex       Age      Time
---------------------------------------------
1        张三        男         20       2008-01
1        李四        女         30       2008-01
1        王五        男         40       2008-02
2        王五        女         20       2008-03给分中........................

解决方案 »

  1.   


    select id=(select count(1) from student where name=a.name and time<=a.time),* from student a
      

  2.   

    咿?
    我明明在线,我的头像怎么是灰的呢?
    破CSDN
      

  3.   

    NND,鸭屁股里拉不出鸵鸟屎来。。(对应“狗嘴里吐不出象牙来”)
      

  4.   

    select id=row_number() over(partition by name order by name),* from student order by time
      

  5.   

    select id =  (select count(1) from student where name = T.name and time <T.time)+1,* 
                from student T