现有一数据表,字段如下: 
组织CODE,卖场CODE,商品CODE 
现想通过SQL语句生态一个字段,取值方法如下: 
组织CODE相同并且卖场CODE也相同记录,按商品CODE的排序,并从1开始给值(相当于是行号) 例: 
---------------------------------- 
组织CODE   卖场CODE    商品CODE   ROWID 
   1       1          1       1 
   1       1          3       2 
   1       1          4       3 
   1       2          1       1 
   2       1          1       1 
----------------------------------

解决方案 »

  1.   

    select
        t.[组织CODE],t.[卖场CODE],t.[商品CODE],
        [ROWID ]=(select count(1) from 数据表 where [组织CODE]=t.[组织CODE] and [卖场CODE]=t.[卖场CODE] and [商品CODE]<=t.[商品CODE]) 
    from
        数据表 t
    order by
        t.[组织CODE],t.[卖场CODE],t.[商品CODE]
      

  2.   

    if object_id('tb') is not null
    drop table tb
    go
    create table tb(组织code int,卖场code int,商品code int)
    insert into tb select 1,1,1
    insert into tb select 1,1,3
    insert into tb select 1,1,4
    insert into tb select 1,2,1
    insert into tb select 2,1,1select *,
    row_number() over(partition by 组织code,卖场code order by 商品code) as rowid
    from tb组织code 卖场code 商品code rowid
    1 1 1 1
    1 1 3 2
    1 1 4 3
    1 2 1 1
    2 1 1 1
      

  3.   

    SELECT *,
    ROWID=(SELECT COUNT(*) FROM TB WHERE 组织CODE=A.组织CODE AND 卖场CODE=A.卖场CODE AND 商品CODE<=A.商品CODE)
    FROM TB A
      

  4.   

    if object_id('tb') is not null
    drop table tb
    go
    create table tb(组织code int,卖场code int,商品code int)
    insert into tb select 1,1,1
    insert into tb select 1,1,3
    insert into tb select 1,1,4
    insert into tb select 1,2,1
    insert into tb select 2,1,1
    --sql2000
    select *,
    ruwid=(select count(1) from tb where 组织code=t.组织code and 卖场code=t.卖场code and 商品code<=t.商品code)
    from tb t组织code 卖场code 商品code ruwid
    1 1 1 1
    1 1 3 2
    1 1 4 3
    1 2 1 1
    2 1 1 1
      

  5.   

    select 组织CODE,卖场CODE,商品CODE,
    (
    select count(*) from buyer br 
    where br.组织CODE =buyer.组织CODE and 
    br.卖场CODE =buyer.卖场CODE and 
    br.商品CODE <=buyer.商品CODE
    ) ROW_ID
    from buyer
    order by 组织CODE,卖场CODE,商品CODE-----------------------------------------
    1 1 1 1
    1 1 3 2
    1 1 4 3
    1 2 1 1
    2 1 1 1
    2 1 2 2
      

  6.   

    select
        t.[组织CODE],t.[卖场CODE],t.[商品CODE],
        [ROWID ]=(select count(1) from 数据表 where [组织CODE]=t.[组织CODE] and [卖场CODE]=t.[卖场CODE] and [商品CODE]<=t.[商品CODE]) 
    from
        数据表 t
    order by
        t.[组织CODE],t.[卖场CODE],t.[商品CODE]