table:  
  name             date               times  
  a             2003.01.01  
  b             2003.01.02  
  a             2003.01.03        
  a             2003.01.04          
  c             2003.01.05        
  c             2003.01.06    
  a             2003.01.07  
  b             2003.01.08    
   
   
  我要根据时间的先后顺序填写各个字母times(次数)的数字  
  该如何写这个sql,没有思路阿?  
   
  填完后的表是:  
  name             date               times  
  a             2003.01.01           1  
  b             2003.01.02           1  
  a             2003.01.03           2  
  a             2003.01.04           3  
  c             2003.01.05           1  
  c             2003.01.06           2  
  a             2003.01.07           4  
  b             2003.01.08           2

解决方案 »

  1.   

    declare @tb table (name varchar(1),date datetime ,times int)
    insert into @tb select 'a','2003.01.01',null
    insert into @tb select 'b','2003.01.02',null
    insert into @tb select 'a','2003.01.03',null
    insert into @tb select 'a','2003.01.04',nullinsert into @tb select 'c','2003.01.05',null
    insert into @tb select 'c','2003.01.06',null
    insert into @tb select 'a','2003.01.07',null
    insert into @tb select 'b','2003.01.08',nullupdate @tb set times=(select count(1) from @tb where name=t.name and date<=t.date) from @tb tselect * from @tb
    name date times
    a 2003-01-01 00:00:00.000 1
    b 2003-01-02 00:00:00.000 1
    a 2003-01-03 00:00:00.000 2
    a 2003-01-04 00:00:00.000 3
    c 2003-01-05 00:00:00.000 1
    c 2003-01-06 00:00:00.000 2
    a 2003-01-07 00:00:00.000 4
    b 2003-01-08 00:00:00.000 2
      

  2.   

    declare @tb table (name varchar(1),date datetime ,times int)
    insert into @tb select 'a','2003.01.01',null
    insert into @tb select 'b','2003.01.02',null
    insert into @tb select 'a','2003.01.03',null
    insert into @tb select 'a','2003.01.04',nullinsert into @tb select 'c','2003.01.05',null
    insert into @tb select 'c','2003.01.06',null
    insert into @tb select 'a','2003.01.07',null
    insert into @tb select 'b','2003.01.08',nullupdate @tb set times=(select count(1) from @tb where name=t.name and date<=t.date) from @tb tselect name,convert(varchar(10),date,120) as date,times from @tbname date times
    a 2003-01-01 1
    b 2003-01-02 1
    a 2003-01-03 2
    a 2003-01-04 3
    c 2003-01-05 1
    c 2003-01-06 2
    a 2003-01-07 4
    b 2003-01-08 2
      

  3.   

    select [name],[date],
    times=(select count(*) from T where name=A.name and [date]<=A.[date])
    from T as A
      

  4.   

    select a.name,a.date,count(1)
    from table a,table b
    where a.name = b.name and a.date>=b.date
    group by a.name,a.date
      

  5.   

    select name , date , times = (select count(1) from tb where name = t.name and date < t.date) + 1 from tb t  
      

  6.   

    create table  tb (name varchar(1),date datetime ,times int)
    insert into tb select 'a','2003.01.01',null
    insert into tb select 'b','2003.01.02',null
    insert into tb select 'a','2003.01.03',null
    insert into tb select 'a','2003.01.04',null
    insert into tb select 'c','2003.01.05',null
    insert into tb select 'c','2003.01.06',null
    insert into tb select 'a','2003.01.07',null
    insert into tb select 'b','2003.01.08',null
    goselect name , date , times = (select count(1) from tb where name = t.name and date < t.date) + 1 from tb t drop table tb/*
    name date                                                   times       
    ---- ------------------------------------------------------ ----------- 
    a    2003-01-01 00:00:00.000                                1
    b    2003-01-02 00:00:00.000                                1
    a    2003-01-03 00:00:00.000                                2
    a    2003-01-04 00:00:00.000                                3
    c    2003-01-05 00:00:00.000                                1
    c    2003-01-06 00:00:00.000                                2
    a    2003-01-07 00:00:00.000                                4
    b    2003-01-08 00:00:00.000                                2(所影响的行数为 8 行)
    */
      

  7.   


    select name,[date],times=(select count(*) from [table] where name=t.name and [date]<=t.[date])
    from [table] t 
      

  8.   

    declare @tb table (name varchar(1),date datetime ,times int)
    insert into @tb select 'a','2003.01.01',null
    insert into @tb select 'a','2003.01.01',null
    insert into @tb select 'a','2003.01.01',null
    insert into @tb select 'a','2003.01.01',null
    insert into @tb select 'a','2003.01.01',null
    insert into @tb select 'b','2003.01.02',null
    insert into @tb select 'a','2003.01.03',null
    insert into @tb select 'a','2003.01.04',nullinsert into @tb select 'c','2003.01.05',null
    insert into @tb select 'c','2003.01.06',null
    insert into @tb select 'a','2003.01.07',null
    insert into @tb select 'b','2003.01.08',nullselect [name],[date], 
    times=(select count(*) from @tb where name=A.name and [date] <=A.[date]) 
    from @tb as A
    /*
    name date                                                   times       
    ---- ------------------------------------------------------ ----------- 
    a    2003-01-01 00:00:00.000                                5
    a    2003-01-01 00:00:00.000                                5
    a    2003-01-01 00:00:00.000                                5
    a    2003-01-01 00:00:00.000                                5
    a    2003-01-01 00:00:00.000                                5
    b    2003-01-02 00:00:00.000                                1
    a    2003-01-03 00:00:00.000                                6
    a    2003-01-04 00:00:00.000                                7
    c    2003-01-05 00:00:00.000                                1
    c    2003-01-06 00:00:00.000                                2
    a    2003-01-07 00:00:00.000                                8
    b    2003-01-08 00:00:00.000                                2
    */
      

  9.   

    declare @tb table (name varchar(1),date datetime ,times int)
    insert into @tb select 'a','2003.01.01',null
    insert into @tb select 'a','2003.01.01',null
    insert into @tb select 'a','2003.01.01',null
    insert into @tb select 'a','2003.01.01',null
    insert into @tb select 'a','2003.01.01',null
    insert into @tb select 'b','2003.01.02',null
    insert into @tb select 'a','2003.01.03',null
    insert into @tb select 'a','2003.01.04',nullinsert into @tb select 'c','2003.01.05',null
    insert into @tb select 'c','2003.01.06',null
    insert into @tb select 'a','2003.01.07',null
    insert into @tb select 'b','2003.01.08',nulldeclare @i int
    set @i = 0
    update @tb set times = @i,@i=@i+1 
    update @tb set times=(select count(*)+1 from @tb where name=A.name and ([date] <A.[date] or (date = a.date and times <a.times)))
    from @tb as A
    /*
    name date                                                   times       
    ---- ------------------------------------------------------ ----------- 
    a    2003-01-01 00:00:00.000                                1
    a    2003-01-01 00:00:00.000                                2
    a    2003-01-01 00:00:00.000                                3
    a    2003-01-01 00:00:00.000                                4
    a    2003-01-01 00:00:00.000                                5
    b    2003-01-02 00:00:00.000                                1
    a    2003-01-03 00:00:00.000                                6
    a    2003-01-04 00:00:00.000                                7
    c    2003-01-05 00:00:00.000                                1
    c    2003-01-06 00:00:00.000                                2
    a    2003-01-07 00:00:00.000                                8
    b    2003-01-08 00:00:00.000                                2
    */
    select * from @tb
      

  10.   

    select name , data , times = (select count(1) from @tb where name = t.name and data < t.data) + 1 from @tb t 
    update @tb set times=(select count(9) from @tb where name=a.name and data<=a.data ) from @tb a
    select * from @tb
      

  11.   

    星辰技术社区:www.netcsharp.cn,我们将帮您以最快的速度找到最佳的解决方案