A表里面有一到十二月每月的数字字段 :要求比如现在是8月份 就求 1到8月之和,如果现在是9月份就求1到9月份之后。该怎么写1    2    3   4   5   6    7    8    9   10   11   12
12  21   22  31  42   52  32   64   23   85   72   97

解决方案 »

  1.   

    select  sum(col) from a  where  month<to_char(sysdate,'YYYY') 
      

  2.   

    假设列名为col_1col_12
    select sum(col_value) from 

    select 1  col_month, col_1  col_value, col_others from tab_name
    union all
    select 2  col_month, col_2  col_value, col_others from tab_name
    union all
    select 3  col_month, col_3  col_value, col_others from tab_name
    union all
    select 4  col_month, col_3  col_value, col_others from tab_name
    union all
    select 5  col_month, col_5  col_value, col_others from tab_name
    union all
    select 6  col_month, col_6  col_value, col_others from tab_name
    union all
    select 7  col_month, col_7  col_value, col_others from tab_name
    union all
    select 8  col_month, col_8  col_value, col_others from tab_name
    union all
    select 9  col_month, col_9  col_value, col_others from tab_name
    union all
    select 10 col_month, col_10 col_value, col_others from tab_name
    union all
    select 11 col_month, col_11 col_value, col_others from tab_name
    union all
    select 12 col_month, col_12 col_value, col_others from tab_name

      where ...... and col_month <= to_number(to_char(sysdate, 'mm')) ;
      

  3.   

    select  sum(val) from a  where  month<=to_char(sysdate,'mm') 
      

  4.   

    SELECT SUM(VAL) FROM A WHERE MONTH <= TO_CHAR(SYSDATE, 'MM');
      

  5.   

    首先把列名改一下,不知妥否我觉得用数字作列名怪怪的,不妨依次叫col1到col12select sum(col_value) as total
    from (
    select col1 as col_value,'1' as flag from mytable
    union 
    select col2 as col_value,'2'  as flag from mytable
    union 
    select col3 as col_value,'3'  as flag from mytable
    union 
    select col4 as col_value,'4'  as flag from mytableunion 
    select col5 as col_value,'5' as flag  from mytable
    union 
    select col6 as col_value,'6' as flag  from mytable
    union 
    select col7 as col_value,'7' as flag  from mytable
    union 
    select col8 as col_value,'8' as flag  from mytableunion 
    select col9 as col_value,'9' as flag  from mytable
    union 
    select col10 as col_value,'10' as flag  from mytable
    union 
    select col11 as col_value,'11' as flag  from mytable
    union 
    select col12 as col_value,'12' as flag  from mytable
    ) a
    where flag<=to_char(sysdate,'MM');