有一个数据库商品        数量汽车001      15
--------------
汽车002      10
--------------
汽车003      18
--------------
飞机001      25
--------------
飞机002      15
--------------
飞机003       5
--------------如果我想把上面的数据变成下面的形式应该怎么做商品        数量       数量多少的排位汽车001      15            2
----------------------------------
汽车002      10            3
----------------------------------
汽车003      18            1
----------------------------------
飞机001      25            1
----------------------------------
飞机002      15            2
----------------------------------
飞机003       5            3
----------------------------------请高手指点,谢谢了。

解决方案 »

  1.   

    DECLARE @a TABLE(商品 varchar(20),       数量  int)
    INSERT @a SELECT '汽车001',      15 
    union all select '汽车002',      10 
    union all select '汽车003',      18 
    union all select '飞机001',      25 
    union all select '飞机002',      15 
    union all select '飞机003',      5 SELECT *,排位=(select COUNT(1)+1 from @a where left(商品,2)=left(a.商品,2) and 数量>a.数量) FROM @a a--result
    /*商品                   数量          排位          
    -------------------- ----------- ----------- 
    汽车001                15          2
    汽车002                10          3
    汽车003                18          1
    飞机001                25          1
    飞机002                15          2
    飞机003                5           3(所影响的行数为 6 行)*/
      

  2.   

    DECLARE @a TABLE(商品 varchar(20),       数量  int)
    INSERT @a SELECT '汽车001',      15 
    union all select '汽车002',      10 
    union all select '汽车003',      18 
    union all select '飞机001',      25 
    union all select '飞机002',      15 
    union all select '飞机003',      5 select 商品,数量,rank() over(PARTITION BY left(商品,2) order by 数量 desc) as 排位 from @a
    /*
    商品                   数量          排位
    -------------------- ----------- --------------------
    飞机001                25          1
    飞机002                15          2
    飞机003                5           3
    汽车003                18          1
    汽车001                15          2
    汽车002                10          3
    */
      

  3.   


    DECLARE @a TABLE(商品 varchar(20),数量  int)
    INSERT @a SELECT '汽車001',      15 
    union all select '汽車002',      10 
    union all select '汽車003',      18 
    union all select '飛機001',      25 
    union all select '飛機002',      15 
    union all select '飛機003',      5 
    select * from @aselect *,rank()over (partition by left(商品,2) order by 数量)[order] from @a
    ------------------------------------------
    商品   数量  order
    ----- ----- ------
    汽車002 10 1
    汽車001 15 2
    汽車003 18 3
    飛機003 5 1
    飛機002 15 2
    飛機001 25 3
      

  4.   

    create table tb(商品 varchar(20), 数量 int)
    INSERT tb SELECT '汽车001', 15 
    union all select '汽车002', 10 
    union all select '汽车003', 18 
    union all select '飞机001', 25 
    union all select '飞机002', 15 
    union all select '飞机003', 5 
    goselect m.商品, m.数量 , 数量多少的排位 = (select count(1) from 
    (
      select left(商品,PATINDEX('%[^吖-做]%',商品)-1) 商品_new ,商品, 数量 from tb
    ) n where n.商品_new = m.商品_new and n.数量 > m.数量 
    ) + 1 from
    (
      select left(商品,PATINDEX('%[^吖-做]%',商品)-1) 商品_new ,商品, 数量 from tb
    ) mdrop table tb/*
    商品                   数量          数量多少的排位     
    -------------------- ----------- ----------- 
    汽车001                15          2
    汽车002                10          3
    汽车003                18          1
    飞机001                25          1
    飞机002                15          2
    飞机003                5           3(所影响的行数为 6 行)
    */
      

  5.   


    declare @数据库 table (商品 varchar(10),数量 int)
    insert into @数据库 select '汽车001',15
              union all select '汽车002',10
              union all select '汽车003',18
              union all select '飞机001',25
              union all select '飞机002',15
              union all select '飞机003',5
    select * ,排拉= (select COUNT(*) from @数据库 where LEFT(商品,2)=LEFT(a.商品,2) and 数量<=a.数量) from @数据库 a
    商品         数量          排拉
    ---------- ----------- -----------
    汽车001      15          2
    汽车002      10          1
    汽车003      18          3
    飞机001      25          3
    飞机002      15          2
    飞机003      5           1(6 行受影响)