Nested aggregation in Ent query

PHPz
Release: 2024-02-09 09:36:20
forward
1101 people have browsed it

Ent 查询中的嵌套聚合

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.

Question content

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
Copy after login

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.

Solution

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)
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!