请问sql组内编号怎么写,比如数据如下  
组名 成员  
一组 a  
一组 b  
二组 c  
二组 d  
期望结果:  
组名 成员 编号  
一组 a     1  
一组 b     2  
二组 c     1  
二组 d     2  
实现组内自动编号,如何实现?

解决方案 »

  1.   

    本帖最后由 roy_88 于 2012-04-16 17:42:34 编辑
      

  2.   

    select *,
    编号=ROW_NUMBER()OVER (partition by 组名 order by 组名)
    from table1或select *,
    编号=row_number()OVER (partition by 组名 order by 成员)
    from table1
      

  3.   


    兄弟上面随手敲漏了()OVER,呵呵
      

  4.   

    CREATE TABLE TB ([组名] NVARCHAR(32), [成员] VARCHAR(12))
    INSERT TB SELECT  '一组','a' UNION ALL  
    SELECT  '一组','b' UNION ALL 
    SELECT  '二组','c' UNION ALL   
    SELECT  '二组','d'select *,
    编号=row_number() over (partition by 组名 order by 组名)
    from TB
    /*
    组名 成员 编号
    二组 c 1
    二组 d 2
    一组 a 1
    一组 b 2
    */ 
      

  5.   

    alter table table1 add 编号 int --新增字段
    --
    go
    update t1
    set 编号=编号2
    from (select *,
    编号2=ROW_NUMBER()OVER (partition by 组名 order by 组名)
    from table1
    )t1
    go
    select * from table1
      

  6.   

    CREATE TABLE TB ([组名] NVARCHAR(32), [成员] VARCHAR(12))
    INSERT TB SELECT  '一组','a' UNION ALL  
    SELECT  '一组','b' UNION ALL 
    SELECT  '二组','c' UNION ALL   
    SELECT  '二组','d'select *,
    编号=row_number() over (partition by 组名 order by 组名)
    from TB
    /*
    组名    成员    编号
    二组    c    1
    二组    d    2
    一组    a    1
    一组    b    2
    */
    ---添加编号到表中alter table tb add [编号] int null
    GO
    update TB set [编号]=[编号2] from (select *,
    编号2=row_number() over (partition by 组名 order by 组名)
    from TB)aaSELECT * FROM TB 
    /*
    组名 成员 编号
    一组 a 1
    一组 b 2
    二组 c 1
    二组 d 2
    */DROP TABLE TB
      

  7.   

    update TB set [编号]=[编号2] from (select *,
    编号2=row_number() over (partition by 组名 order by 组名)
    from TB)aa赋值出来的编号全部都是“1”值
      

  8.   

    组内只有一个成员的话:
    编号2=row_number() over (partition by 组名 order by 组名)
    就是都是 1 
    你的题目不要求的就是这样吗
    如果不按分组产生序号就不是1 了就是1.....n 到你的实际行数的行号了
    就应该这样了:编号2=row_number() over ( order by 组名)
    或 编号2=row_number() over ( order by 成员)
      

  9.   

    我再说明下我的题目吧,估计表述不是很清楚
    请问sql组内编号怎么写,比如数据如下   
    组名 成员   
    一组 a   
    一组 b   
    二组 c   
    二组 d   
    期望结果:   
    组名 成员 编号   
    一组 a    1   
    一组 a    2  
    一组 b    1  
    一组 b    2
    二组 c    1   
    二组 d    2   
    实现组内自动编号,如何实现?直接插入到编号
      

  10.   

    我再说明下我的题目吧,估计表述不是很清楚
    请问sql组内编号怎么写,比如数据如下   
    组名 成员   
    一组  a    
    一组  a    
    一组  b    
    一组  b 
    二组  c    
    二组  d   
    期望结果:   
    组名 成员 编号   
    一组 a    1   
    一组 a    2   
    一组 b    1   
    一组 b    2
    二组 c    1   
    二组 d    2   
    实现组内自动编号,如何实现?直接插入到编号 
      

  11.   

    update SYQDJ
    set 编号=编号2
    from (select   
    编号2=ROW_NUMBER()OVER (partition by DJQGD,DJZQGD order by DJQGD)
    from SYQDJ
    )t1
    用这个语句就不行吗?得到的都是1,select   
    编号2=ROW_NUMBER()OVER (partition by DJQGD,DJZQGD order by DJQGD)
    from SYQDJ
    但是查询出来的结果是对的。
      

  12.   


    --> 测试数据:[tbl]
    if object_id('[tbl]') is not null drop table [tbl]
    create table [tbl]([组名] varchar(4),[成员] varchar(1))
    insert [tbl]
    select '一组','a' union all
    select '一组','a' union all
    select '一组','b' union all
    select '一组','b' union all
    select '二组','c' union all
    select '二组','d'go
    alter table tbl add id int
    go
    alter table tbl add row int identity(1,1)
    go
    update tbl set id=row_num from(
    select *,
    row_num=(select COUNT(1) from tbl where 组名=a.组名 and 成员=a.成员 and row<=a.row)
    from tbl as a)b where tbl.row=b.row
    go
    alter table tbl drop column row
    go
    select * from tbl你看看这个能不能行,汗,给你回复了你瞄都不瞄一眼
      

  13.   


    --> 测试数据:[tbl]
    if object_id('[tbl]') is not null drop table [tbl]
    create table [tbl]([组名] varchar(4),[成员] varchar(1))
    insert [tbl]
    select '一组','a' union all
    select '一组','a' union all
    select '一组','b' union all
    select '一组','b' union all
    select '二组','c' union all
    select '二组','d'
    --2000中的方法:
    go
    alter table tbl add id int
    go
    alter table tbl add row int identity(1,1)
    go
    update tbl set id=row_num from(
    select *,
    row_num=(select COUNT(1) from tbl where 组名=a.组名 and 成员=a.成员 and row<=a.row)
    from tbl as a)b where tbl.row=b.row
    go
    alter table tbl drop column row
    go
    select * from tbl
    --2005以上版本的方法:
    go
    alter table tbl add row int identity(1,1)--增加标识列,用来更新时确定一一对应
    go
    ;with t 
    as(
    select s=ROW_NUMBER()over(partition by [组名],[成员] order by getdate()),
    * from tbl
    )
    update tbl set tbl.id=t.s from t
    where t.row=tbl.row
    go
    alter table tbl drop column row
    select * from tbl
    /*
    组名 成员 id
    一组 a 1
    一组 a 2
    一组 b 1
    一组 b 2
    二组 c 1
    二组 d 1
    */
      

  14.   


    你更新之所以错误是因为where后面的关系并不能确保一一对应,比如说
    一组   a   
    一组   a
    这个按照之前的关系根就不是确保了一对一的更新
      

  15.   

    CREATE TABLE TB ([组名] NVARCHAR(32), [成员] VARCHAR(12))
    INSERT TB SELECT  '一组','a' UNION ALL  
    SELECT  '一组','b' UNION ALL 
    SELECT  '二组','c' UNION ALL   
    SELECT  '二组','d'SELECT *,ROW_NUMBER() OVER(PARTITION BY [组名] ORDER BY [组名] DESC) NUM FROM TB
      

  16.   

    这样?
    --> --> (Roy)生成測試數據
     
    if not object_id('Tempdb..#T') is null
    drop table #T
    Go
    Create table #T([组名] nvarchar(2),[成员] nvarchar(1))
    Insert #T
    select N'一组',N'a' union all
    select N'一组',N'b' union all
    select N'二组',N'c' union all
    select N'二组',N'd'
    Go
    alter table #T add 编号 int --新增字段
    --
    go
    update t1
    set 编号=编号2
    from (select *,
    编号2=ROW_NUMBER()OVER (partition by 组名 order by 组名)
    from #T
    )t1
    go
    select * from #T/*
    组名 成员 编号
    一组 a 1
    一组 b 2
    二组 c 1
    二组 d 2
    */
      

  17.   


    --> 测试数据:[tbl]
    if object_id('[tbl]') is not null drop table [tbl]
    create table [tbl]([组名] varchar(4),[成员] varchar(1))
    insert [tbl]
    select '一组','a' union all
    select '一组','a' union all
    select '一组','b' union all
    select '一组','b' union all
    select '二组','c' union all
    select '二组','d'
    --2000中的方法:
    go
    alter table tbl add id int
    go
    alter table tbl add row int identity(1,1)
    go
    update tbl set id=row_num from(
    select *,
    row_num=(select COUNT(1) from tbl where 组名=a.组名 and 成员=a.成员 and row<=a.row)
    from tbl as a)b where tbl.row=b.row
    go
    alter table tbl drop column row
    go
    select * from tbl
    --2005以上版本的方法:
    go
    alter table tbl add row int identity(1,1)--增加标识列,用来更新时确定一一对应
    go
    ;with t 
    as(
    select s=ROW_NUMBER()over(partition by [组名],[成员] order by getdate()),
    * from tbl
    )
    update tbl set tbl.id=t.s from t
    where t.row=tbl.row
    go
    alter table tbl drop column row
    select * from tbl
    /*
    组名    成员    id
    一组    a    1
    一组    a    2
    一组    b    1
    一组    b    2
    二组    c    1
    二组    d    1
    */
      

  18.   

    update syqdj set 编号=row_num from(
    select *,
    row_num=(select COUNT(1) from syqdj where DJQGD=a.DJQGD and DJZQGD=a.DJZQGD and ywbh<=a.ywbh)
    from syqdj as a)b where syqdj.ywbh=b.ywbh
    go非常感谢。可以了
      

  19.   

    select *,编号=row_number(partition by 组名 order by 组名) from yourtable