2017-09-26 1 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 

에 두 개의 추가 열을 만드는 것입니다 그 주와 음식점을위한 음식과 음료를위한 식사 기간 당 수익의 %

그래서 예를 들어, 타코 벨에 대한 주 하나는 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)