2017-09-26 7 views
0

列の情報に基づいて合計の割合を持つ列を追加する方法を見つけようとしています。私はそれが可能だと思うが、私はそれを行う方法を把握することはできません。ここでTeradata SQLの合計の割合に対する列を追加します。

が私の(正常に動作します)SQLコードとテーブル結果の簡易版である:

select week 
, restaurant 
, case 
    when cast(timestamp as time format 'HH:MI:SS') between '03:00:01' and '10:00:00' then 'Breakfast' 
    when cast(timestamp as time format 'HH:MI:SS') between '10:00:01' and '14:59:00' then 'Lunch' 
    else 'Dinner' 
    end 
    as meal 
, sum(revenue) as total_rev 
, sum(case when product_type = 'food' then revenue else 0 end) as food_rev 
, sum(case when product_type = 'bev' then revenue else 0 end) bev_rev 

from table1 
group by 1,2,3 
order by 1,2,3 

表出力

week restaurant meal  total_rev food_rev bev_rev 
1  Taco Bell Breakfast 300  200  100 
1  Taco Bell Lunch  250  210  40 
1  Taco Bell Dinner  450  250  200 
1  McDonalds Breakfast 100  70  30 
1  McDonalds Lunch  150  100  50 
1  McDonalds Dinner  250  130  120 
2  Taco Bell Breakfast 200  120  80 
2  Taco Bell Lunch  150  110  40 
2  Taco Bell Dinner  350  240  110 
2  McDonalds Breakfast 200  70  130 
2  McDonalds Lunch  250  120  130 
2  McDonalds Dinner  150  100  50 

はへの2つの追加の列を私は何をしたいを作成していますその週およびレストランのための食事および飲料のための食事の期間あたりの収入のパーセント。

だから例えば、タコベルのために週1は、0.3030(200 /(200 + 210 + 250)。そしてそうで。

week restaurant meal  total_rev food_rev per_food bev_rev per_bev 
1  Taco Bell Breakfast 300  200  .3030  100  .2941 
1  Taco Bell Lunch  250  210  .3182  40  .1176 
1  Taco Bell Dinner  450  250  .3788  200  .5882 
1  McDonalds Breakfast 100  70     30 
1  McDonalds Lunch  150  100    50 
1  McDonalds Dinner  250  130    120 
2  Taco Bell Breakfast 200  120    80 
2  Taco Bell Lunch  150  110    40 
2  Taco Bell Dinner  350  240    110 
2  McDonalds Breakfast 200  70     130 
2  McDonalds Lunch  250  120    130 
2  McDonalds Dinner  150  100    50 

答えて

0

あなたが利用できるように朝食用食品の割合を持っているでしょうグループ合計:このような週/レストランの合計を取得する:

100.00 * food_rev/sum(food_rev) over (partition by week restaurant) 
関連する問題