2017-01-12 13 views
-1

行間の動的平均値の計算方法は?行間の動的平均値の計算方法は?

最初の12か月status_flagはNになり、13ヵ月目以降は、最初の13行の平均販売数を最小値と最大値と比較し、最小値と最大値の間にある場合はstatus_flagYとし、それ以外の場合はNと設定します。

同じ14行目は、最初の14行の平均をとり、最小値と最大値と比較します。

これを行う方法?

+0

OracleまたはMS SQL Serverを使用していますか? (含まれていない製品にはタグを付けないでください) – jarlh

+0

私はOracleを使用していますが、この質問は一般的です。 Oracle固有のものではありません。 @ jarlh – general46

+0

あなたの質問はちょっと混乱しています...言い換えてみてください。 – Tobias

答えて

0
Update table t set status_flag = 
    case when 
    (Select count(*) 
     From table 
     where month <= t.Month) > 12 
    and 
    (select avg(sales) 
     from table 
     where Month <= t.Month) 
    Between Min and Max 
    then 'Y' else 'N' end 
1

私は挑戦的な部分は平均的な売上を得ることだと思います。 Analytic Functions:

select Storeid, Months, Min, Max, sales, 
    avg(sales) over (order by Months RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as avg_sales 
from your_table; 

残りはもっと簡単になります。デフォルト値はRANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROWなので、スキップすることができます。

with a as 
    (select Storeid, Months, Min, Max, sales, 
     avg(sales) over (order by Months) as avg_sales 
    from your_table) 
select Storeid, Months, Min, Max, sales, avg_sales, 
    case 
     when Months <= 12 then 'N' 
    else 
     case 
     when avg_sales between Min and Max then 'Y' 
     else 'N' 
     end 
    end as Status_flag 
from a; 
関連する問題