2017-10-14 80 views
0

我想創建一個列,根據商店列和小時列獲取總小時數。見下表。因此,它會從商店142和總代表1,2從商店356總量可達rep1,2,3然後我也想devide小時後總拿到了貢獻%列SQL Sumif語句請求

Date store rep hours total cont% 
    -------------------------------------------------- 
    x 142 rep1 5 11 0.45 
    x 142 rep2 2 11 0.18 
    x 142 rep3 4 11 0.36 
    x 356 rep1 4  7 0.57 
    x 356 rep2 3  7 0.42 

謝謝!

回答

0

你想窗口功能:

select t.*, sum(hours) over (partition by store) as total, 
     t.hours * 1.0/sum(hours) over (partition by store) as cont_percent 
from t; 
+0

真棒!哇,這很快。第二部分的貢獻%你在完成編輯之前最初的回答!再次感謝! –