数据表如下:产品名称  2月销量排名  1月销量 2月销量  3月销量  4月销量  5月销量  6月销量   7月销量   8月销量...  12月销量
有没有函数能将第二字段(2月销量排名)的值求出来,【2月销量排名】是2月在一年12个月中的销量排名

解决方案 »

  1.   

    --table为表名 
    --p_mon月份参数 为1月,2月.........12月
    --如果lz只是想求出2月的  那就把p_mon写死吧
    create or replace function fun(p_mon in varchar2) return number is
      Result number;
    declare 
    p_rank number;
    begin
      select rn into p_rank
      from (select mon, rownum rn
              from (select mon
                      from (select '1月' mon, 1月销量 sale
                              from table
                            union all
                            select '2月' mon, 2月销量 sale
                              from table
                            union all
                            select '3月' mon, 3月销量 sale
                              from table
                            union all
                            select '4月' mon, 4月销量 sale
                              from table
                            union all
                            select '5月' mon, 5月销量 sale
                              from table
                            union all
                            select '6月' mon, 6月销量 sale
                              from table
                            union all
                            select '7月' mon, 7月销量 sale
                              from table
                            union all
                            select '8月' mon, 8月销量 sale
                              from table
                            union all
                            select '9月' mon, 9月销量 sale
                              from table
                            union all
                            select '10月' mon, 10月销量 sale
                              from table
                            union all
                            select '11月' mon, 11月销量 sale
                              from table
                            union all
                            select '12月' mon, 12月销量 sale from table)
                     order by sal desc))
     where mon = p_mon;  return p_rank;
    end fun;
      

  2.   

    2月销量排名=(row_number() over(partition by 商品 order by 2月销量/总销售量 desc) 排名)--2月销量/总销售量 是要从你统计的结果作为一个表来用 不能直接sum(2月销量)之类的
      

  3.   

    看来大家对我的描述还是有异议,那我举个具体的例子吧,为了方面,下面只列举3个月的tSaleCount表结构产品名称  1月销量 2月销量 3月销量
      AA        100    200    300
      BB        500    400    600
      CC        300    700    200
      DD        500    400    600我要的结果产品名称  1月销量 2月销量 3月销量   二月销量排行
      AA        100    200    300        2        --AA商品1-3月份数据中,2月排名第二   
      BB        500    400    600        3        --BB商品1-3月份数据中,3月排名第二
      CC        300    700    200        1
      DD        500    400    600        3
      

  4.   

    看来大家对我的描述还是有异议,那我举个具体的例子吧,为了方面,下面只列举3个月的tSaleCount表结构产品名称 1月销量 2月销量 3月销量
    --AA--------100-----200-----300
    --BB--------500-----400-----600
    --CC--------300-----700-----200
    --DD--------500-----400-----600我要的结果产品名称  1月销量  2月销量 3月销量 二月销量排行
    --AA--------100-----200-----300-----2    (AA商品1-3月份数据中,2月排名第2)   
    --BB--------500-----400-----600-----3    (BB商品1-3月份数据中,3月排名第3)
    --CC--------300-----700-----200-----1
    --DD--------500-----400-----600-----3
      

  5.   

    AA  2月销量为200 排名应该为第四吧?
    BB  怎么出来了3月排名 不是2月的吗? 
      

  6.   

    sorry  aa 这个2月排名是3个月之间的排名
    那bb怎么回事呀?
      

  7.   

    改造下 2楼的  
    with temp as
    select 产品名称 row_number() over(partition by  产品名称 order by sale) rn from (
    select  产品名称, 2月销量 sale , '1' flag  from your_table 
    union all 
    select  产品名称, 1月销量, '0' flag  from your_table 
    union all 
    select  产品名称, 3月销量, '0' flag  from your_table 
    union all 
    select  产品名称, 4月销量, '0' flag  from your_table 
    union all 
    select  产品名称, 5月销量, '0' flag  from your_table 
    union all 
    select  产品名称, 6月销量, '0' flag  from your_table 
    union all 
    select  产品名称, 7月销量, '0' flag  from your_table 
    union all 
    select  产品名称, 8月销量, '0' flag  from your_table 
    union all 
    select  产品名称, 9月销量, '0' flag  from your_table 
    union all 
    select  产品名称, 10月销量, '0' flag  from your_table 
    union all 
    select  产品名称, 11月销量, '0' flag  from your_table 
    union all 
    select  产品名称, 12月销量, '0' flag  from your_table 
    )
    where flag = '1'
    select a.* ,b.rn 2月排名 from your_table a, temp b
    where a.产品名称 = b.产品名称在家里 没法试验 不知道这样行不行 
      

  8.   

    同一个月,不同上面之间的销量排名,也就是纵向排名,数据库有rank()可以实现,所以我没有说出来,我要的是同一个商品,不同月份之间的横向排名,我没有发现数据有什么函数
      

  9.   

    明白了 
    没有系统函数可以实现的 
    你可以采用1楼上面的写法 不过要加上group by 按产品名称分组 如果很多年份的话 也要加上年份分组 就可以满足lz的需求
      

  10.   

    把这种写法的sql改下 通过rank进行排行吧
      

  11.   

    SQL> select * from tSaleCount t;
     
    PRODUCT_NAME                JAN        FEB        MAR
    -------------------- ---------- ---------- ----------
    A                           100        200        300
    B                           500        400        600
    C                           300        700        200
    D                           500        400        600SQL> create or replace function p_rank(p1 number,p2 number,p3 number)
      2  return number
      3  is
      4  rank number;
      5  begin
      6  select rn into rank
      7  from
      8  (
      9  select quantity,row_number() over(order by quantity desc) rn
     10  from
     11  (
     12  select p1 quantity
     13  from dual
     14  union all
     15  select p2 quantity
     16  from dual
     17  union all
     18  select p3 quantity
     19  from dual
     20  )
     21  )
     22  where quantity = p2;
     23  return rank;
     24  end;
     25  /
     
    Function created
     
    SQL> select t.*,p_rank(jan,feb,mar) from tSaleCount t;
     
    PRODUCT_NAME                JAN        FEB        MAR P_RANK(JAN,FEB,MAR)
    -------------------- ---------- ---------- ---------- -------------------
    A                           100        200        300                   2
    B                           500        400        600                   3
    C                           300        700        200                   1
    D                           500        400        600                   3