多行过滤器并计算百分比

时间:2020-09-27 14:42:40

标签: sql sql-server sum pivot percentage

enter image description here我是SQL的新手。我正在处理一个需要过滤然后计算百分比的数据库。下面是示例表。 遵循的步骤:

  • 第一个过滤器的“列性质”,仅具有“创建”,“查看”和“坐标”。
  • 按专栏,团队,TeamTeam,客户分组并汇总时间。
  • 然后根据上述组计算创建,审核和协调的百分比。
  • 最后,我需要输出列Team,TeamTeam,Client,%Create,%Review和%Coordinate。

先谢谢了。 enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用where子句进行过滤,然后使用group bysum。要计算百分比,一种简单的方法是avg()和条件聚合。

select team, client, sum(hours) sum_hours,
    avg(case when nature = 'create'     then 100.0 else 0 end) percent_create,
    avg(case when nature = 'review'     then 100.0 else 0 end) percent_review,
    avg(case when nature = 'coordinate' then 100.0 else 0 end) percent_coordinate
from mytable
where nature in ('create', 'review', 'coordinate')
group by team, client
相关问题