Home >Backend Development >Golang >Nested aggregation in Ent query
php editor Apple will introduce you to "nested aggregation in Ent query" in this article. In data query and analysis, nested aggregation is a powerful technique that can perform multiple levels of aggregation operations in a single query. By using nested aggregations, we can conduct in-depth analysis of data more flexibly and get more accurate results. This article will explain in detail what nested aggregation is and how to implement nested aggregation operations in the Ent framework to help readers better understand and apply this technology.
How to write this simple sql statement using the code generated by ent?
select max(t.sum_score) from (select sum(score) as "sum_score" from matches group by team) as t
I tried using the custom sql modifier feature flags described here, but I don't know how to access the sum_score
field from outside the modifier.
This is the answer from ent project owner a8m (Thank you!)
client.Match.Query(). Aggregate(func(s *sql.Selector) string { const as = "max_score" s.GroupBy(match.FieldTeam).OrderBy(sql.Desc(as)).Limit(1) return sql.As(sql.Sum(match.FieldScore), as) }). IntX(ctx)
You can find the full answer here on the official github repository.
I had to add sql.desc(as)
to get the maximum value.
The above is the detailed content of Nested aggregation in Ent query. For more information, please follow other related articles on the PHP Chinese website!