新标题:Calculate the average of each group relative to the total sum
P粉852578075
P粉852578075 2023-12-28 16:05:46
0
2
316

I want to get the sum of group A and group B respectively, and then divide by the sum.

I tried using this:

select name, sum(qt)
from ntbl
group by name
order_id Name qt
1 one 12
2 one 20
3 B 33
4 B 45

The result should be:

Name qt dv
one 32 0.29
B 78 0.70


P粉852578075
P粉852578075

reply all(2)
P粉561438407

You can cross-join another subquery that sums all quantities

dbviolinhere p>

P粉005417748

You can combine aggregate functions and window functions together:

select name
     , sum(qt) as sum_qt
     , sum(qt) / sum(sum(qt)) over () * 100 as pct_qt
from t
group by name
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!