![](/img/trans.png)
[英]how to use the result of 2 columns to create calculated column in Oracle sql
[英]How to create calculated columns in SQL with sql-window functions?
所以我正在研究这个解决方案,它涉及获取 3 列的输入,比如“年”、“月”和“花费”,并使用它们使用模仿 SQL 窗口函数的逻辑创建另一列“年总计”。 例如,我需要下表,因为前三列是可编辑的,
|------------|-------|--------|----------| | Year |Month | Spend |Year Total| |------------|-------|--------|----------| | 2018 | Jan | 10 | 55 | | 2018 | Feb | 20 | 55 | | 2018 | Oct | 25 | 55 | | 2019 | Jan | 15 | 120 | | 2019 | Aug | 60 | 120 | | 2019 | Nov | 45 | 120 | |------------|-------|--------|----------|
但是在前端和动态计算这个会使解决方案变得非常缓慢。 因此,我试图使用计算列将此计算移动到 SQL 表中的后端。 基本上,数据将写入后端(SQL),计算列并重新读取以显示在前端。
我环顾了一下,才发现windows函数不能用在SQL的计算列中。 另请注意,解决方案中不允许使用存储过程。
任何人都可以建议如何做到这一点,或者即使这是可能的?
请注意,前端是 Powerapps/Power BI
“使用模仿 SQL 窗口函数的逻辑”为什么?
您只需要加入按年份分组的子查询。
select t.* , s.sumspend
from <table> t
join (select year, sum(spend) sumspend from <table> group by year) s
on s.year = t.year;
使用视图:
create view v_t as
select t.* ,
sum(spend) over (partition by year) as year_spend
from t;
生成的列中不允许使用窗口函数。
SQL Server 允许可更新视图中的窗口函数,因此您将能够修改其他列。 每当发生更改时,您都需要重新加载表,以获取更新的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.