方案1 BL-643942 .00
方案1 BL-643943 .00
方案1 WS-250001 .00
方案1 ZPK-280004 .00
方案1 ZPK-280015 .00
方案1 WS-250006 .00
方案1 ZS-650007 .00
方案2 BL-643942 .00
方案2 BL-643943 .00
方案2 WS-250001 .00
方案2 ZPK-280015 .00
方案2 ZPK-280004 .00
方案2 WS-250006 .00
方案2 ZS-650007 .00
……
如何得到
方案1 1 BL-643942 .00
方案1 2 BL-643943 .00
方案1 3 WS-250001 .00
方案1 4 ZPK-280004 .00
方案1 5 ZPK-280015 .00
方案1 6 WS-250006 .00
方案1 7 ZS-650007 .00
方案2 1 BL-643942 .00
方案2 2 BL-643943 .00
方案2 3 WS-250001 .00
方案2 4 ZPK-280015 .00
方案2 5 ZPK-280004 .00
方案2 6 WS-250006 .00
方案2 7 ZS-650007 .00
……

解决方案 »

  1.   

    --> 测试数据: #T
    if object_id('tempdb.dbo.#T') is not null drop table #T
    create table #T (C1 varchar(11),C2 varchar(13))
    insert into #T
    select '方案1','BL-643942.00' union all
    select '方案1','BL-643943.00' union all
    select '方案1','WS-250001.00' union all
    select '方案1','ZPK-280004.00' union all
    select '方案1','ZPK-280015.00' union all
    select '方案1','WS-250006.00' union all
    select '方案1','ZS-650007.00' union all
    select '方案2','BL-643942.00' union all
    select '方案2','BL-643943.00' union all
    select '方案2','WS-250001.00' union all
    select '方案2','ZPK-280015.00' union all
    select '方案2','ZPK-280004.00' union all
    select '方案2','WS-250006.00' union all
    select '方案2','ZS-650007.00'select C1,Cn=row_number()over(partition by C1 order by C1),C2 from #T
    /*
    C1          Cn                   C2
    ----------- -------------------- -------------
    方案1         1                    BL-643942.00
    方案1         2                    BL-643943.00
    方案1         3                    WS-250001.00
    方案1         4                    ZPK-280004.00
    方案1         5                    ZPK-280015.00
    方案1         6                    WS-250006.00
    方案1         7                    ZS-650007.00
    方案2         1                    BL-643942.00
    方案2         2                    BL-643943.00
    方案2         3                    WS-250001.00
    方案2         4                    ZPK-280015.00
    方案2         5                    ZPK-280004.00
    方案2         6                    WS-250006.00
    方案2         7                    ZS-650007.00
    */
      

  2.   

    --> --> (Ben)生成測試數據
     
    if not object_id('Tempdb..#T') is null
    drop table #T
    Go
    Create table #T([name] nvarchar(3),[cd] nvarchar(10),[val] decimal(18,2))
    Insert #T
    select '方案1','BL-643942',.00 union all
    select '方案1','BL-643943',.00 union all
    select '方案1','WS-250001',.00 union all
    select '方案1','ZPK-280004',.00 union all
    select '方案1','ZPK-280015',.00 union all
    select '方案1','WS-250006',.00 union all
    select '方案1','ZS-650007',.00 union all
    select '方案2','BL-643942',.00 union all
    select '方案2','BL-643943',.00 union all
    select '方案2','WS-250001',.00 union all
    select '方案2','ZPK-280015',.00 union all
    select '方案2','ZPK-280004',.00 union all
    select '方案2','WS-250006',.00 union all
    select '方案2','ZS-650007',.00
    Go
    select name,row_number() over(partition by name order by name),cd,val from #T
      

  3.   

    if not object_id('Tempdb..#T') is null
        drop table #T
    Go
    Create table #T([name] nvarchar(3),[cd] nvarchar(10),[val] decimal(18,2))
    Insert #T
    select '方案1','BL-643942',.00 union all
    select '方案1','BL-643943',.00 union all
    select '方案1','WS-250001',.00 union all
    select '方案1','ZPK-280004',.00 union all
    select '方案1','ZPK-280015',.00 union all
    select '方案1','WS-250006',.00 union all
    select '方案1','ZS-650007',.00 union all
    select '方案2','BL-643942',.00 union all
    select '方案2','BL-643943',.00 union all
    select '方案2','WS-250001',.00 union all
    select '方案2','ZPK-280015',.00 union all
    select '方案2','ZPK-280004',.00 union all
    select '方案2','WS-250006',.00 union all
    select '方案2','ZS-650007',.00
    Goselect id=identity(int,1,1) ,* into #table1 from #T where name='方案1'
    select id=identity(int,1,1) ,* into #table2 from #T where name='方案2'
    select * from #table1
    union select * from #table2 order by name
      

  4.   

    select name,row_number() over(partition by name order by name),cd,val from #T
    oracle 的写法