Having created this view:
class OpenPositionMetric(Base):
stmt = (
select(
[
OpenPosition.belongs_to.label("belongs_to"),
OpenPosition.account_number.label("account_number"),
OpenPosition.exchange.label("exchange"),
OpenPosition.symbol.label("symbol"),
round(OpenPosition.actual_shares * OpenPosition.avg_cost_per_share,3).label(
"cost_value"
),
]
)
.select_from(OpenPosition)
.order_by("belongs_to", "account_number", "exchange", "symbol")
)
view = create_materialized_view(
name="vw_open_positions_metrics",
selectable=stmt,
metadata=Base.metadata,
indexes=None,
)
__table__ = view
I get the example result for the field cost_value: 1067.2500060000000000.
Is there a way to limit the number of decimal digits for that view field?
The function round() doesn't work. Maybe because round is a python function and SqlAlchemy is expecting and sql expression language function like func.sum?
Update:
I've found a solution but it isn't pretty. I'm sure there is a better one...
(text("ROUND (operations.tb_open_positions.actual_shares * operations.tb_open_positions.avg_cost_per_share,3) AS cost_value"))),
The value above is now displayed in the view as 1067.250