Skip to content

Commit 02f25dd

Browse files
committed
Doc: Fix code error
1 parent 9e5de73 commit 02f25dd

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

sum_column_data_in_sql.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ from sqlalchemy import func
6767
@app.route('/customer-contribution')
6868
def customer_contribution():
6969
contributions = db.session.query(
70-
Contribution.name, func.sum(
71-
Contribution.contribution)).group_by(Contribution.name).all()
70+
Contribution.customer_id, func.sum(
71+
Contribution.contribution)).group_by(Contribution.customer_id).all()
7272
return render_template('customer_data.html', contributions=contributions)
7373
```
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.name`. 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.name)`. 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 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`.
7878

7979

8080
## Display The Data
@@ -96,7 +96,7 @@ We can display an individual customer's data on an HTML template as follows:
9696
{% for contribution in contributions %}
9797
{% if contribution %}
9898
<tr>
99-
<td>{{ contribution.name }}</td>
99+
<td>{{ contribution.customer.name }}</td>
100100
<td>{{ contribution[1] }}</td>
101101
</tr>
102102
{% endif %}
@@ -106,4 +106,4 @@ We can display an individual customer's data on an HTML template as follows:
106106
</div>
107107
```
108108

109-
We loop through the `contributions` list to access the individual data we want. The Jinja2 templating engine allows for the use of double curly braces `{{ }}` to pass in dynamic data (data that can change). Remember, the customer's name can be gotten from `{{ contribution.name }}`. His total contribution is not found in the database, so we can use an index to access the sum. The conditional `if` statement has been used to check if there is data in our query. If none exists, then nothing will be shown.
109+
We loop through the `contributions` list to access the individual data we want. The Jinja2 templating engine allows for the use of double curly braces `{{ }}` to pass in dynamic data (data that can change). Remember, the customer's name can be gotten from `{{ contribution.customer.name }}`. His total contribution is not found in the database, so we can use an index to access the sum. The conditional `if` statement has been used to check if there is data in our query. If none exists, then nothing will be shown.

0 commit comments

Comments
 (0)