![](/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.