You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: ratio_to_report.md
+18-6Lines changed: 18 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Calculating percentages using RATIO_TO_REPORT
2
2
3
-
RATIO_TO_REPORT Window function can be used to calculate percentage of the entire dataset or partitioned data.
3
+
`RATIO_TO_REPORT` Window function can be used to calculate percentage of the entire dataset or partitioned data.
4
4
5
5
## An illustrative example
6
6
@@ -19,7 +19,7 @@ from soccer_players
19
19
20
20
Our data is about a soccer game where each row represents a player id, with columns for id, their team, and how many goals they have scored.
21
21
22
-
If we wanted to work out what percentage of all goal each player has scored, we can use ratio_to_report() window function as following:
22
+
If we wanted to work out what percentage of all goal each player has scored, we can use `ratio_to_report()` window function as following:
23
23
24
24
```sql
25
25
select
@@ -35,12 +35,13 @@ order by team, position;
35
35

36
36
37
37
38
-
The over() in the query above tells the database to expand the ‘window’ in which the ratio_to_report() operates to the whole dataset.
38
+
The `over()` in the query above tells the database to expand the _window_ in which the `ratio_to_report()` operates to the whole dataset.
39
39
40
-
The ratio_to_report() window function takes an expression (usually a column) as input and calculates the ratio of that expression to the window that is defined (in this case, the whole dataset).
41
-
Using ratio_to_report() to Calculate Partitioned Percentages
40
+
The `ratio_to_report()` window function takes an expression (usually a column) as input and calculates the ratio of that expression to the window that is defined (in this case, the whole dataset).
42
41
43
-
Let’s say we were not only interested in each players overall contribution to goals, but also their contribution to goals for their specific team. We can then add a partition by to the window:
42
+
## Using ratio_to_report() to Calculate Partitioned Percentages
43
+
44
+
Let’s say we were not only interested in each players overall contribution to goals, but also their contribution to goals for their specific team. We can then add a partition by to the _window_:
44
45
45
46
```sql
46
47
select
@@ -54,3 +55,14 @@ order by team, position;
54
55
```
55
56
56
57

0 commit comments