JeyJ
JeyJ

Reputation: 4070

kql window query sum over partition

I have the following table :

Group UserId count_
1 2 2
1 1 3
2 3 3
2 4 7

I want to run a sum() over partition by group in order to calculate the total requests for every group and add a percentage column for every user in the group.

The expected output :

Group UserId percent
1 2 0.4
1 1 0.6
2 3 0.3
2 4 0.7

In SQL i would do something like the following :

select group,user,count_/sum(count_) over(partition by group) from table

How can i get this output ?

Upvotes: 0

Views: 664

Answers (1)

David דודו Markovitz
David דודו Markovitz

Reputation: 44941

At least at this point, a JOIN is needed (similarly to a SQL solution without the use of windows functions)

let t = datatable(Group:int, UserId:int, count:int)
[
    1 ,2 ,2
   ,1 ,1 ,3
   ,2 ,3 ,3
   ,2 ,4 ,7
];
t
| summarize sum(['count']) by Group
| join kind=inner t on Group 
| project Group, UserId, percent = 1.0*['count']/sum_count
Group UserId percent
1 2 0.4
1 1 0.6
2 3 0.3
2 4 0.7

Fiddle

Upvotes: 2

Related Questions