sn       lineno  stockcode    qty
100007    1      A123113       5
100007    2      A123113       22
100007    3      A123113       33
100007    4      A123113       4
100007    5      A123114       5444
100007    6      A123113       55
100007    7      A123114       5643
-->查询结果
100007    1      A123113       sum(qty)
100007    2      A123114       sum(qty)我想查到同一个sn里 相同料号的数量合计,怎样自动添加新的行号?

解决方案 »

  1.   

    select m.sn , 
           lineno = (select count(1) from (select sn , stockcode , sum(qty) qty from tb group by sn , stockcode) n where n.sn < m.sn or (n.sn = m.sn and n.stockcode < m.stockcode)) + 1,
           m.stockcode,
           m.qty
    from
    (
      select sn , stockcode , sum(qty) qty from tb group by sn , stockcode
    ) m
      

  2.   

    --sql 2000create table tb(sn varchar(10), [lineno] int, stockcode varchar(10), qty int)
    insert into tb values('100007' , 1 , 'A123113' , 5 )
    insert into tb values('100007' , 2 , 'A123113' , 22) 
    insert into tb values('100007' , 3 , 'A123113' , 33) 
    insert into tb values('100007' , 4 , 'A123113' , 4) 
    insert into tb values('100007' , 5 , 'A123114' , 5444) 
    insert into tb values('100007' , 6 , 'A123113' , 55) 
    insert into tb values('100007' , 7 , 'A123114' , 5643)
    goselect m.sn , 
           [lineno] = (select count(1) from (select sn , stockcode , sum(qty) qty from tb group by sn , stockcode) n where n.sn < m.sn or (n.sn = m.sn and n.stockcode < m.stockcode)) + 1,
           m.stockcode,
           m.qty
    from
    (
      select sn , stockcode , sum(qty) qty from tb group by sn , stockcode
    ) m
     
    drop table tb/*
    sn         lineno      stockcode  qty         
    ---------- ----------- ---------- ----------- 
    100007     1           A123113    119
    100007     2           A123114    11087(所影响的行数为 2 行)
    */
      

  3.   

    --sql 2005create table tb(sn varchar(10), [lineno] int, stockcode varchar(10), qty int)
    insert into tb values('100007' , 1 , 'A123113' , 5 )
    insert into tb values('100007' , 2 , 'A123113' , 22) 
    insert into tb values('100007' , 3 , 'A123113' , 33) 
    insert into tb values('100007' , 4 , 'A123113' , 4) 
    insert into tb values('100007' , 5 , 'A123114' , 5444) 
    insert into tb values('100007' , 6 , 'A123113' , 55) 
    insert into tb values('100007' , 7 , 'A123114' , 5643)
    goselect m.sn , 
           [lineno] = row_number() over(order by sn , stockcode) ,
           m.stockcode,
           m.qty
    from
    (
      select sn , stockcode , sum(qty) qty from tb group by sn , stockcode
    ) m
     
    drop table tb/*
    sn         lineno               stockcode  qty
    ---------- -------------------- ---------- -----------
    100007     1                    A123113    119
    100007     2                    A123114    11087(2 行受影响)
    */
      

  4.   

    create table tb_test
    (
      sn nvarchar(20),
      [lineno] int,
       stockcoce nvarchar(20),
       qty int
    )insert into tb_test
    select '100007',    1 ,     'A123113',      5
    union all
    select '100007',    2,      'A123113',      22 
    union all
    select '100007',    3,      'A123113',      33 
    union all
    select '100007',    4,      'A123113',      4 
    union all
    select '100007',    5,      'A123114',      5444
    union all
    select '100007',    6,      'A123113',      55 
    union all
    select '100007',    7,      'A123114',      5643 select * from tb_testselect max(sn) as sn,row_number() over (order by stockcoce) as  [lineno]  , stockcoce,sum(qty)as qty from tb_test group by stockcocesn                   lineno               stockcoce            qty
    -------------------- -------------------- -------------------- -----------
    100007               1                    A123113              119
    100007               2                    A123114              11087(2 row(s) affected)
      

  5.   


    select m.sn , 
           [lineno] = row_number() over(order by sn , stockcode) ,
           m.stockcode,
           sum(m.qty)
    from
    tb m
    group by sn,stockcode
      

  6.   

    谢谢dawugui,
    可以根据sn将行号分开吗?
    有其它sn的时候,这个行号是整个sn自动增加的.1000007 1
    1000007 21000008 1
    1000008 2
      

  7.   

    我更改了其中一条记录为1000008--sql 2000
    create table tb(sn varchar(10), [lineno] int, stockcode varchar(10), qty int)
    insert into tb values('100007' , 1 , 'A123113' , 5 )
    insert into tb values('100007' , 2 , 'A123113' , 22) 
    insert into tb values('100007' , 3 , 'A123113' , 33) 
    insert into tb values('100008' , 4 , 'A123113' , 4) 
    insert into tb values('100007' , 5 , 'A123114' , 5444) 
    insert into tb values('100007' , 6 , 'A123113' , 55) 
    insert into tb values('100007' , 7 , 'A123114' , 5643)
    goselect m.sn , 
           [lineno] = (select count(1) from (select sn , stockcode , sum(qty) qty from tb group by sn , stockcode) n where n.sn = m.sn and n.stockcode < m.stockcode) + 1,
           m.stockcode,
           m.qty
    from
    (
      select sn , stockcode , sum(qty) qty from tb group by sn , stockcode
    ) m
    order by sn , [lineno]
     
    drop table tb/*
    sn         lineno      stockcode  qty         
    ---------- ----------- ---------- ----------- 
    100007     1           A123113    115
    100007     2           A123114    11087
    100008     1           A123113    4(所影响的行数为 3 行)
    */
    --sql 2005
    create table tb(sn varchar(10), [lineno] int, stockcode varchar(10), qty int)
    insert into tb values('100007' , 1 , 'A123113' , 5 )
    insert into tb values('100007' , 2 , 'A123113' , 22) 
    insert into tb values('100007' , 3 , 'A123113' , 33) 
    insert into tb values('100008' , 4 , 'A123113' , 4) 
    insert into tb values('100007' , 5 , 'A123114' , 5444) 
    insert into tb values('100007' , 6 , 'A123113' , 55) 
    insert into tb values('100007' , 7 , 'A123114' , 5643)
    goselect m.sn , 
           [lineno] = row_number() over(partition by sn order by stockcode) ,
           m.stockcode,
           m.qty
    from
    (
      select sn , stockcode , sum(qty) qty from tb group by sn , stockcode
    ) m
    order by sn ,[lineno] 
    drop table tb/*
    sn         lineno               stockcode  qty
    ---------- -------------------- ---------- -----------
    100007     1                    A123113    115
    100007     2                    A123114    11087
    100008     1                    A123113    4(3 行受影响)
    */
      

  8.   

    use Tempdb
    go
    --> --> 
     
    if not object_id('Tempdb..#T') is null
    drop table #T
    Go
    Create table #T([sn] int,[lineno] int,[stockcode] nvarchar(7),[qty] int)
    Insert #T
    select 100007,1,N'A123113',5 union all
    select 100007,2,N'A123113',22 union all
    select 100007,3,N'A123113',33 union all
    select 100007,4,N'A123113',4 union all
    select 100007,5,N'A123114',5444 union all
    select 100007,6,N'A123113',55 union all
    select 100007,7,N'A123114',5643
    Go
    Select [sn],row_number()over(partition by [sn] order by sum([qty]) )[lineno],[stockcode],sum([qty]) as [qty] from #T group by [sn],[stockcode]
    sn          lineno               stockcode qty
    ----------- -------------------- --------- -----------
    100007      1                    A123113   119
    100007      2                    A123114   11087(2 個資料列受到影響)
      

  9.   

    --> (让你望见影子的墙)生成测试数据,时间:2009-03-27
     
    if not object_id('tb') is null
    drop table tb
    Go
    Create table tb([sn] int,[lineno] int,[stockcode] nvarchar(7),[qty] int)
    Insert tb
    select 100007,1,N'A123113',5 union all
    select 100007,2,N'A123113',22 union all
    select 100007,3,N'A123113',33 union all
    select 100007,4,N'A123113',4 union all
    select 100007,5,N'A123114',5444 union all
    select 100007,6,N'A123113',55 union all
    select 100007,7,N'A123114',5643 union all
    select 100008,8,N'A123115',232 union all
    select 100008,9,N'A123456',234
    Go
    Select * from tbselect m.sn , 
           [lineno] = row_number() over(order by sn , stockcode) ,
           m.stockcode,
           sum(m.qty)
    from
    tb m
    group by sn,stockcode
    100007 1 A123113 119
    100007 2 A123114 11087
    100008 3 A123115 232
    100008 4 A123456 234
      

  10.   


    该一下
    select m.sn , 
           [lineno] = row_number() over(partition by sn order by sn ) ,
           m.stockcode,
           sum(m.qty)
    from
    tb m
    group by sn,stockcode100007 1 A123113 119
    100007 2 A123114 11087
    100008 1 A123115 232
    100008 2 A123456 234
      

  11.   

    select sn,stockcode,[lineno]=(select count(1) from (select sn,stockcode,sum(qty) qty  from tb group by sn,stockcode) b where b.sn=a.sn and b.stockcode<a.stockcode)+1,qty from 
    (select sn,stockcode,sum(qty) qty  from tb group by sn,stockcode) a
      

  12.   


    select m.sn , 
           [lineno] = row_number() over(partition by sn order by sn ) ,
           m.stockcode,
           sum(m.qty)
    from
    tb m
    group by sn,stockcode
      

  13.   

    row_number() over(partition by sn order by sn )学习了。
      

  14.   

    create table tb(sn varchar(10), [lineno] int, stockcode varchar(10), qty int)
    insert into tb values('100007' , 1 , 'A123113' , 5 )
    insert into tb values('100007' , 2 , 'A123113' , 22) 
    insert into tb values('100007' , 3 , 'A123113' , 33) 
    insert into tb values('100007' , 4 , 'A123113' , 4) 
    insert into tb values('100007' , 5 , 'A123114' , 5444) 
    insert into tb values('100007' , 6 , 'A123113' , 55) 
    insert into tb values('100007' , 7 , 'A123114' , 5643)
    go
    select sn,stockcode,Sum(qty) as 总量 from tb group by sn,stockcodedrop table tb感觉比较简单啊,楼上的怎么这么复杂