billno    id       itemno    price
10000     20060    ESD_01    1.30
10000     20068    ESD_23    52.30
10002     12521    ESD_98    12.20
10002     12521    ESD_98    15.23
10003     12554    FRD_52    125.22
........這是一張票具的表體,billno id itemno 聯合主健,billno 關聯表頭,要求結果集如下:billno    id       itemno    price
10000     1    ESD_01    1.30
10000     2    ESD_23    52.30
10002     1    ESD_98    12.20
10002     2    ESD_98    15.23
10003     1    FRD_52    125.22求sql語句一條,多謝

解决方案 »

  1.   


    declare @tab table(billno int,id int,itemno varchar(20),price decimal(10,2))insert @tab values(10000,20060,'ESD_01',1.30)
    insert @tab values(10000,20068,'ESD_23',52.30)
    insert @tab values(10002,12521,'ESD_98',12.20)
    insert @tab values(10002,12521,'ESD_98',15.23)
    insert @tab values(10003,12554,'FRD_52',125.22)select billno,id=(select count(1)+1 from @tab where billno=a.billno and price<a.price),itemno,price from @tab a
      

  2.   

    ----呵呵,即使价格一样也可以
    declare @tab table(billno int,id int,itemno varchar(20),price decimal(10,2))insert @tab values(10000,20060,'ESD_01',1.30)
    insert @tab values(10000,20068,'ESD_23',52.30)
    insert @tab values(10002,12521,'ESD_98',12.20)
    insert @tab values(10002,12521,'ESD_98',15.23)
    insert @tab values(10003,12554,'FRD_52',125.22)select billno,
    id=(select count(1)+1 from @tab where billno=a.billno and (price<a.price or itemno<a.itemno)),itemno,price 
    from @tab a  
    *****************************************************************************
      

  3.   

    我这个不需要什么比较。。只要有这么多行就要这么多序号。。换句话说。。没什么可以比较的。。
    ------------------------
    即使你的需求没要求比较同BillNo行
    但是,为了得到序列值,需要我们在算法上建立不同行比较
      
    *****************************************************************************
      

  4.   

    问题在于。我同一个billno底下的数据除了GID
    要求不同之外。。其他的都可以相同。。也就是其他的可能完全一样
      

  5.   

    按billno分组排序?有一表
    a    b        c
    7    aa      153
    9    aa      152
    6    aa      120
    8    aa      168
    5    bb      159
    7    bb      179
    8    bb      149
    9    bb      139
    6    bb      169
    对b列中的值来分类排序并分别加一序号,形成一新表
    px a    b        c
    1  6    aa      120
    2  9    aa      152
    3  7    aa      153
    4  8    aa      168
    1  9    bb      139
    2  8    bb      149
    3  5    bb      159
    4  6    bb      169
    5  7    bb      179
    declare @tab table(a int,b varchar(2),c int)insert @tab values(7,'aa',153)
    insert @tab values(9,'aa',152)
    insert @tab values(6,'aa',120)
    insert @tab values(8,'aa',168)
    insert @tab values(5,'bb',159)
    insert @tab values(7,'bb',179)
    insert @tab values(8,'bb',149)
    insert @tab values(9,'bb',139)
    insert @tab values(6,'bb',169)select * from @tabselect px=(select count(1) from @tab where b=a.b and c<a.c)+1 , a,b,c from @tab a
    order by b , c px          a           b    c           
    ----------- ----------- ---- ----------- 
    1           6           aa   120
    2           9           aa   152
    3           7           aa   153
    4           8           aa   168
    1           9           bb   139
    2           8           bb   149
    3           5           bb   159
    4           6           bb   169
    5           7           bb   179(所影响的行数为 9 行)
    在上面例中我们看到,以B分类排序,C是从小到大,如果C从大到小排序,即下面结果:
    px a    b        c
    1  8    aa      168
    2  9    aa      153
    3  7    aa      152
    4  6    aa      120
    1  7    bb      179
    2  6    bb      169
    3  5    bb      159
    4  8    bb      149
    5  9    bb      139declare @tab table(a int,b varchar(2),c int)insert @tab values(7,'aa',153)
    insert @tab values(9,'aa',152)
    insert @tab values(6,'aa',120)
    insert @tab values(8,'aa',168)
    insert @tab values(5,'bb',159)
    insert @tab values(7,'bb',179)
    insert @tab values(8,'bb',149)
    insert @tab values(9,'bb',139)
    insert @tab values(6,'bb',169)select * from @tabselect px=(select count(1) from @tab where b=a.b and c>a.c)+1 , a,b,c from @tab a
    order by b , c descpx          a           b    c           
    ----------- ----------- ---- ----------- 
    1           8           aa   168
    2           7           aa   153
    3           9           aa   152
    4           6           aa   120
    1           7           bb   179
    2           6           bb   169
    3           5           bb   159
    4           8           bb   149
    5           9           bb   139(所影响的行数为 9 行)
      

  6.   

    if object_id('pubs..tb') is not null
       drop table tb
    gocreate table tb
    (
    billno varchar(10),
    id varchar(10),
    itemno varchar(10),
    price decimal(18,2)
    )insert into tb(billno,id,itemno,price) values('10000','20060','ESD_01',1.30)
    insert into tb(billno,id,itemno,price) values('10000','20068','ESD_23',52.30)
    insert into tb(billno,id,itemno,price) values('10002','12521','ESD_98',12.20)
    insert into tb(billno,id,itemno,price) values('10002','12521','ESD_98',15.23)
    insert into tb(billno,id,itemno,price) values('10003','12554','FRD_52',125.22)select billno , id=(select count(1) from tb where billno=a.billno and price<a.price)+1 , itemno,price from tb a
    order by billno , pricedrop table tbbillno     id          itemno     price                
    ---------- ----------- ---------- -------------------- 
    10000      1           ESD_01     1.30
    10000      2           ESD_23     52.30
    10002      1           ESD_98     12.20
    10002      2           ESD_98     15.23
    10003      1           FRD_52     125.22(所影响的行数为 5 行)
      

  7.   

    --下面是有相同值的.if object_id('pubs..tb') is not null
       drop table tb
    gocreate table tb
    (
    billno varchar(10),
    id varchar(10),
    itemno varchar(10),
    price decimal(18,2)
    )insert into tb(billno,id,itemno,price) values('10000','20060','ESD_01',1.30)
    insert into tb(billno,id,itemno,price) values('10000','20068','ESD_23',1.30)
    insert into tb(billno,id,itemno,price) values('10002','12521','ESD_98',12.20)
    insert into tb(billno,id,itemno,price) values('10002','12521','ESD_98',15.23)
    insert into tb(billno,id,itemno,price) values('10003','12554','FRD_52',125.22)select billno , id=(select count(1) from tb where billno=a.billno and price<a.price)+1 , itemno,price from tb a
    order by billno , pricedrop table tbbillno     id          itemno     price                
    ---------- ----------- ---------- -------------------- 
    10000      1           ESD_01     1.30
    10000      1           ESD_23     1.30
    10002      1           ESD_98     12.20
    10002      2           ESD_98     15.23
    10003      1           FRD_52     125.22(所影响的行数为 5 行)
      

  8.   

    顶一下。。楼上的哥哥的答案有点出入。。我的要求是如果有相同的值的。。也要billno 
        id          itemno     price                
    ---------- ----------- ---------- -------------------- 
    10000      1           ESD_01     1.30
    10000      2           ESD_23     1.30
    10002      1           ESD_98     12.20有 1,2 这样拍下来