Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
micpst committed Aug 30, 2024
1 parent 1199f73 commit 3d705ee
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
34 changes: 15 additions & 19 deletions docs/quickstart/aggregations.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,26 @@ class CandidateView(SqlAlchemyBaseView):
Returns the number of candidates per position per country.
"""
return (
self.select
.with_only_columns(
sqlalchemy.func.count(Candidate.position).label("number_of_candidates"),
Candidate.position,
Candidate.country,
)
.group_by(Candidate.position, Candidate.country)
.order_by(sqlalchemy.desc("number_of_candidates"))
self.select.with_only_columns(
sqlalchemy.func.count(Candidate.position).label("number_of_positions"),
Candidate.position,
Candidate.country,
)
.group_by(Candidate.position, Candidate.country)
.order_by(sqlalchemy.desc("number_of_positions"))
)

@decorators.view_aggregation()
def top_universities(self, limit: int) -> sqlalchemy.Select:
def candidates_per_country(self) -> sqlalchemy.Select:
"""
Returns the top universities by the number of candidates.
Returns the number of candidates per country.
"""
return (
self.select
.with_only_columns(
sqlalchemy.func.count(Candidate.id).label("number_of_candidates"),
Candidate.university,
)
.group_by(Candidate.university)
.order_by(sqlalchemy.desc("number_of_candidates"))
.limit(limit)
self.select.with_only_columns(
sqlalchemy.func.count(Candidate.id).label("number_of_candidates"),
Candidate.country,
)
.group_by(Candidate.country)
)
```

Expand Down Expand Up @@ -86,7 +82,7 @@ Number of rows: 1
```
</details>

Feel free to try other questions like: "What's the distribution of candidates across different positions and countries?" or "What are the top 3 universities with the most candidates?".
Feel free to try other questions like: "What's the distribution of candidates across different positions and countries?" or "How many candidates are from China?".

## Full Example

Expand Down
21 changes: 8 additions & 13 deletions examples/aggregations.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,23 @@ def positions_per_country(self) -> sqlalchemy.Select:
"""
return (
self.select.with_only_columns(
sqlalchemy.func.count(Candidate.position).label("number_of_candidates"),
sqlalchemy.func.count(Candidate.position).label("number_of_positions"),
Candidate.position,
Candidate.country,
)
.group_by(Candidate.position, Candidate.country)
.order_by(sqlalchemy.desc("number_of_candidates"))
.order_by(sqlalchemy.desc("number_of_positions"))
)

@decorators.view_aggregation()
def top_universities(self, limit: int) -> sqlalchemy.Select:
def candidates_per_country(self) -> sqlalchemy.Select:
"""
Returns the top universities by the number of candidates.
Returns the number of candidates per country.
"""
return (
self.select.with_only_columns(
sqlalchemy.func.count(Candidate.id).label("number_of_candidates"),
Candidate.university,
)
.group_by(Candidate.university)
.order_by(sqlalchemy.desc("number_of_candidates"))
.limit(limit)
)
return self.select.with_only_columns(
sqlalchemy.func.count(Candidate.id).label("number_of_candidates"),
Candidate.country,
).group_by(Candidate.country)


async def main() -> None:
Expand Down

0 comments on commit 3d705ee

Please sign in to comment.