【问题标题】:New column computed from another columns in the declaration - Views - SqlAlchemy从声明中的其他列计算的新列 - 视图 - SqlAlchemy
【发布时间】:2021-01-14 05:05:53
【问题描述】:

我有这个类在 PostgreSQL 中创建物化视图:

class TotalProfit(ExecutionsBase):
    stmt = (
        select(
            [
                TotalRealizedCapital.symbolExchange.label("symbolExchange"),
                TotalRealizedCapital.symbol.label("symbol"),
                TotalRealizedCapital.currency.label("currency"),
                TotalRealizedCapital.totalRealizedCapital.label("totalRealizedCap"),
                OpenPosition.totalUnrealizedCapital.label("totalUnrealizedCap"), #- simplified  
                Dividend.totalDividends.label("totalDividends"), #- simplified
                ("totalRealizedCap" + "totalDividends" + "totalUnrealizedCap").label("total"),
            ]
        )
        .select_from(TotalRealizedCapital)
        .order_by("symbolExchange", "symbol", "currency")
    )

    view = create_materialized_view(
        name="vw_total_profit",
        selectable=stmt,
        metadata=ExecutionsBase.metadata,
        indexes=None,
    )
    __table__ = view

我想添加一个 total 列,它是 先前列的总和: 总计 = totalRealizedCap + totalDividends + totalUnrealizedCap

我已经搜索了使用 Computed() 和 column_property() 的方法,但它似乎在可选范围内没有用。

有没有办法根据其他标签列的值创建一个新的标签列?


更新:

似乎没有办法在同一个选择(数据库约束)中引用别名。一些建议使用 db variables,另一些建议使用 子查询,另一些建议使用 CTE

我正在试图弄清楚如何翻译这种可能性,使用 select 语句来创建一个带有 SqlAlchemy 的视图。

【问题讨论】:

  • 我也尝试过使用 literal_column("totalRealizedCap") 的解决方案,但它指的是原始表上的列,而不是正在创建的视图的列。

标签: postgresql view sqlalchemy label


【解决方案1】:

CTE 解决方案:

class TotalProfit(ExecutionsBase):
    first_cte = (
        select(
            [
                TotalRealizedCapital.symbolExchange.label("symbolExchange"),
                TotalRealizedCapital.symbol.label("symbol"),
                TotalRealizedCapital.totalRealizedCapital.label("totalRealizedCap"),
                OpenPosition.totalUnrealizedCapital.label(
                    "totalUnrealizedCap"
                ),
                Dividend.totalDividends.label("totalDividends"),
            ]
        )
        .select_from(TotalRealizedCapital)
        .order_by("symbolExchange", "symbol")
        .cte()
    )

    stmt = (
        select(
            [
                TotalRealizedCapital.symbolExchange.label("symbolExchange"),
                TotalRealizedCapital.symbol.label("symbol"),
                first_cte.c.totalRealizedCap,
                first_cte.c.totalUnrealizedCap,
                first_cte.c.totalDividends,
                (
                    first_cte.c.totalRealizedCap
                    + first_cte.c.totalUnrealizedCap
                    + first_cte.c.totalDividends
                ).label("total"),
            ]
        )
        .select_from(TotalRealizedCapital)
        .where(
            (TotalRealizedCapital.symbolExchange == first_cte.c.symbolExchange)
            & (TotalRealizedCapital.symbol == first_cte.c.symbol)
        )
        .order_by("symbolExchange", "symbol")
    )

    view = create_materialized_view(
        name="vw_total_profit",
        selectable=stmt,
        metadata=ExecutionsBase.metadata,
        indexes=None,
    )
    __table__ = view

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-13
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 2015-06-02
    相关资源
    最近更新 更多