从表中每行除以同一表的总和(Divide from table each row with the sum of the same table)

编程入门 行业动态 更新时间:2024-10-28 20:29:21
从表中每行除以同一表的总和(Divide from table each row with the sum of the same table)

我有一个显示事件的查询,对于这个事件有持续时间。 为了获得持续时间,我在查询中进行了计算:

Sum(cast(EventEndDateTime - EventStartDateTime as float)) as Duration,

桌子:

Event Duration [Pr 10,48 [Al 9,89 [To 1,32 [Co 0,41 [Gh 0,33

而且我必须将此行的每一行与列持续时间的总和相除。 例如:列持续时间的总和= 22,43。 - >(10,48 / 22,43)* 100 = 46,72%

我试过这个:

Sum(cast(EventEndDateTime - EventStartDateTime as float)) / (SELECT Sum(cast(EventEndDateTime - EventStartDateTime as float)) From Event) as SumOfDuration

但这给了我不正确的结果。 我使用SSRS

I have got a query that shows Events and for this events there are durations. To get the Duration I did a calculation in my query:

Sum(cast(EventEndDateTime - EventStartDateTime as float)) as Duration,

The Table:

Event Duration [Pr 10,48 [Al 9,89 [To 1,32 [Co 0,41 [Gh 0,33

And I must divide each of this row with the sum of the column Duration. For Example: The Sum of the column Duration is =22,43. --> (10,48/22,43)*100 = 46,72%

I tried this here:

Sum(cast(EventEndDateTime - EventStartDateTime as float)) / (SELECT Sum(cast(EventEndDateTime - EventStartDateTime as float)) From Event) as SumOfDuration

But this gave me not the correct result. I use SSRS

最满意答案

SQL方法:

select event, duration, duration/sum(duration) over (partition by 1) percent from ( select event, sum(cast(EventEndDateTime - EventStartDateTime as float)) as Duration from t group by event) as t

SQL approach:

select event, duration, duration/sum(duration) over (partition by 1) percent from ( select event, sum(cast(EventEndDateTime - EventStartDateTime as float)) as Duration from t group by event) as t

更多推荐

本文发布于:2023-08-04 23:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423116.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:总和   Divide   table   row   sum

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!