Skip to content

Commit 7592557

Browse files
committed
Doc: Update content
1 parent 02f25dd commit 7592557

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

sum_column_data_in_sql.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def customer_contribution():
7474

7575
SQLAchemy provides the `func` namespace that is used to invoke SQL functions. In our case above, we want to invoke the `sum` function and apply it to all customer contributions. So, what is going on here? If you remember from above, it is a customer's name that keeps appearing multiple times. This customer can, and certainly does, make multiple contributions.
7676

77-
We begin by specifying this `name` column from the `Contribution` model as `Contribution.customer_id`. This is possible because both models are related as specified by the `relationship` method found in the `Customer` model. Using `func`, we invoke the `sum` method on the `contribution` column of the `Contribution` model. Intentionally, this adds all data in the `contribution` column. But before we can finish, we want to ensure that the addition applies only to the said customer whose name keeps appearing multiple times. Therefore, we combine his contribution by using `group_by(Contribution.customer_id)`. What we have so far is the total contribution of a single customer. The `.all()` method is applied to return all customers in the database. In the end, we will have a list with customers' data such as `[('Gitau', 100), ('Njeri', 2200), ('Muthoni', 400)]`. This list is passed to the templates as a variable called `contributions`.
77+
We begin by specifying a customer by using his `id` column from the `Contribution` model as `Contribution.customer_id`. This is possible because both models are related as specified by the `relationship` method found in the `Customer` model. Using `func`, we invoke the `sum` method on the `contribution` column of the `Contribution` model. Intentionally, this adds all data in the `contribution` column. But before we can finish, we want to ensure that the addition applies only to the said customer whose name keeps appearing multiple times. Therefore, we combine his contribution by using `group_by(Contribution.customer_id)`. What we have so far is the total contribution of a single customer. The `.all()` method is applied to return all customers in the database. In the end, we will have a list with customers' data such as `[('Gitau', 100), ('Njeri', 2200), ('Muthoni', 400)]` assuming that Gitau has an `id=1`, Njeri has `id=2` and Muthoni has `id=3`. This list is passed to the templates as a variable called `contributions`.
7878

7979

8080
## Display The Data

0 commit comments

Comments
 (0)