本帖最后由 hongyuan20022003 于 2010-11-16 14:56:50 编辑

解决方案 »

  1.   


    --> 数据库版本:
    --> Microsoft SQL Server 2008 (RTM) - 10.0.1600.22
    --> 测试数据:[TB]
    IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[TB]') 
    AND type in (N'U')) 
    DROP TABLE [TB]
    GO---->建表
    create table [TB]([id] varchar(2),[date] varchar(4),[num] int)
    insert [TB]
    select '01','9-1',2 union all
    select '01','9-11',3 union all
    select '01','9-22',4 union all
    select '01','10-1',1 union all
    select '01','11-1',3 union all
    select '01','11-3',4
    GO
    SELECT *,SUBSTRING(date,1,CHARINDEX('-',date)-1) 
     FROM [TB] t
    --> 查询结果
    SELECT *,月份=(select COUNT(1) from TB where 
    SUBSTRING(date,1,CHARINDEX('-',date)-1)
    =SUBSTRING(t.date,1,CHARINDEX('-',t.date)-1) and num<=t.num 
    group by SUBSTRING(date,1,CHARINDEX('-',date)-1)) 
     FROM [TB] t
    --> 删除表格
    --DROP TABLE [TB]
      

  2.   

    --> 测试数据: #tb
    if object_id('tempdb.dbo.#tb') is not null drop table #tb
    go
    create table #tb (id varchar(2),date varchar(4))
    insert into #tb
    select '01','9-1' union all
    select '01','9-11' union all
    select '01','9-22' union all
    select '01','10-1' union all
    select '01','11-1' union all
    select '01','11-3'select *, 
    id=dense_rank()over( order by cast(left(date,charindex('-',date)-1) as int))
     from #tb
    id   date id
    ---- ---- --------------------
    01   9-1  1
    01   9-11 1
    01   9-22 1
    01   10-1 2
    01   11-1 3
    01   11-3 3(6 行受影响)
      

  3.   

    1 2010-09-12          
    2 2010-09-20          
    3 2010-09-27          
    4 2010-10-05          
    5 2010-10-25          
    6 2010-11-01          
    7 2010-11-09          
    8 2011-01-14          
    9 2011-11-28          
    Select str2,id=dense_rank() over (order by datepart(year,str2),datepart(month,str2)) from tb2
      

  4.   

    2005及以上版本用dense_rank()这个排名函数就可以了