【问题标题】:Port Postgresql to sea-query将 Postgresql 移植到 sea-query
【发布时间】:2022-08-04 04:12:57
【问题描述】:

我正在尝试将 Postgres 查询移植到 sea-queryin Rust。由于我是sea-query的新手,我已经到了不知道如何移植以下 SQL 代码的地步:

WITH agg_data AS
         (SELECT tableB_fk
               , tableB.name
               , MAX(version)   maxversion
               , SUM(downloads) sumdownloads
          FROM table1
                   INNER JOIN tableB on tableB.id = tableA.tableB_fk
          GROUP BY tableB.name, tableB_fk)
SELECT ad.*
     , t2.created
     , t2.downloads
FROM agg_data ad
         JOIN tableA t2 ON t2.version = ad.maxversion AND t2.tableB_fk = ad.tableB_fk;

我无法弄清楚withsub-select 的查询是如何在sea-query 中完成的。不幸的是,文档中没有 with 子句的示例。

欢迎任何帮助!

标签: postgresql sea-orm sea-query


【解决方案1】:

以下工作并产生与上面相同的字符串。

let base_query = sea_orm::sea_query::SelectStatement::new()
            .column(tableB_fk)
            .expr(Expr::tbl(tableB, tableB.name))
            .expr_as(
                Expr::tbl(
                    tableA,
                    tableA.Version,
                )
                .max(),
                Alias::new("maxversion"),
            )
            .expr_as(
                Expr::tbl(
                    tableA,
                    tableA.Downloads,
                )
                .sum()
                .cast_as(Alias::new("bigint")),
                Alias::new("sumdownloads"),
            )
            .from(tableA)
            .inner_join(
                tableB,
                Expr::tbl(tableB, tableB.Id).equals(
                    tableA,
                    tableB_fk,
                ),
            )
            .add_group_by(vec![
                Expr::col((tableB, tableB.name)).into(),
                Expr::col(tableB_fk).into(),
            ])
            .to_owned();

        let common_table_expression = CommonTableExpression::new()
            .query(base_query)
            .table_name(Alias::new("agg_data"))
            .to_owned();

        let select = sea_orm::sea_query::SelectStatement::new()
            .expr(Expr::expr(SimpleExpr::Custom("ad.*".to_string())))
            .column((Alias::new("t2"), tableA.Created))
            .column((Alias::new("t2"), tableA.Downloads))
            .from_as(Alias::new("agg_data"), Alias::new("ad"))
            .join_as(
                JoinType::Join,
                tableA,
                Alias::new("t2"),
                Condition::all()
                    .add(
                        Expr::tbl(Alias::new("t2"), tableA.Version)
                            .equals(Alias::new("ad"), Alias::new("maxversion")),
                    )
                    .add(
                        Expr::tbl(Alias::new("t2"), tableB_fk)
                            .equals(Alias::new("t2"), tableB_fk),
                    ),
            )
            .to_owned();

        let with_clause = WithClause::new().cte(common_table_expression).to_owned();

        let query = select.with(with_clause).to_owned();

        let qs = query.to_string(PostgresQueryBuilder);
        println!("QUERY STRING: {qs}");

【讨论】:

    猜你喜欢
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    相关资源
    最近更新 更多