SQL Sumif语句请求

时间:2017-10-14 03:18:26

标签: sql sql-server

我想创建一个列,该列将根据store列和hours列获取总小时数。见下表。因此它将从商店142总共获得1,2,3和来自商店356的总代表1,2.然后我还想花费数小时来获得贡献%列

   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

谢谢!

1 个答案:

答案 0 :(得分: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;
相关问题