問題是這樣的。我有兩個表
Transaction_Table
| TradeID | 購買日期 | 。賣出日期 | |||||
|---|---|---|---|---|---|---|---|
| 1234T | 12/04/2002 | 1235T | |||||
| 1235T | 11/05/2020 | 11/05/2020 | 30/08/2020 | 1236T | 15/07/2010 | 17/01/2020 | 17/01/2020 |
| 1237T? | 19/12/2020 | 19/12/2020 |
估價表(該表包含所有日期和所有交易的估價)
| 交易ID | 。估價日期 | 估值||
|---|---|---|---|
| 1234T | 01/01/2020 | 01/01/2020 | 100英鎊 |
| 2020年1月1日 | 100英鎊 | ||
| 1234T? | 31/12/2020 | 31/12/2020 |
我試圖根據交易的買入和賣出時間來計算交易的估值變動,該財政年度是指2020年1月1日至2020年12月31日。 規則如下:
我在選擇陳述句中用CASE將估值開始和結束日期添加到交易表中,就像這樣
。 Select *
Case[/span
When t. 購買date < 1/01/2020 and (t. sell date is not null or t. 出售date > 31/12/2020) THEN 01/01/2020)
When t. 購買date < 1/01/2020 and t。 出售date < 31/12/2020 THEN t。 賣掉date
When t. 購買 date > 1/01/2020 and (t. sell date is not null or t. 出售date > 31/12/2020) THEN 31/12/2020)
When t。 購買 date > 1/01/2020 and (t. sell date is not null or t. 出售date < 31/12/2020) THEN t.賣出 date
END as [Start Date ]
Case] Case
When t. 購買date < 1/01/2020 and (t. sell date is not null or t. 出售date > 31/12/2020) THEN 31/12/2020)
When t。 購買date < 1/01/2020 and t。 出售date < 31/12/2020 THEN t。 賣掉date
When t. 購買 date > 1/01/2020 and (t. sell date is not null or t. 出售date > 31/12/2020) THEN 31/12/2020)
When t。 購買 date > 1/01/2020 and (t. sell date is not null or t. 出售date < 31/12/2020) THEN t.賣出 date
END as [End Date]
FROM Transaction_table t
這將創建以下表格
我現在糾結的是如何根據選擇陳述句中創建的開始和結束日期從估值表中添加估值。問題似乎是這些列只是通過選擇陳述句而存在的,并不存在于原始交易表中。
最終的結果應該是這樣的
uj5u.com熱心網友回復:
如果你需要使用計算列的結果,只需使用派生表子查詢(我所使用的CTE是其中一種形式)。
有趣的是,你發布的查詢充滿了語法錯誤。你是否真的得到了它的作業?
正如philipxy所指出的,你的日期格式是模糊的,可能會導致問題。但我還是把這個問題留給你來解決吧。
我還建議不要在你的列名中使用空格,因為你最終不得不將它們全部轉義。
with cte1 as (
Select *
, Case *
When t. [purchase date] < '1/01/2020' and (t. [sell date] is not null or T. [sell date] > '31/12/2020') THEN '01/01/2020')
When t.[購買date] < '1/01/2020' and t. [sell date] < '31/12/2020' THEN t. [selldate]
When t. [purchase date] > '1/01/2020' and (t. [sell date] is not null or T. [sell date] > '31/12/2020') THEN '31/12/2020')
When t. [purchase date] > '1/01/2020' and (t. [sell date] is not null or T. [sell date] < '31/12/2020') THEN t. [sell date]
END as [Start Date]
Case] ,Case。
When t. [purchase date] < '1/01/2020' and (t. [sell date] is not null or T. [sell date] > '31/12/2020') THEN '31/12/2020')
When t.[購買date] < '1/01/2020' and t. [sell date] < '31/12/2020' THEN t. [selldate]
When t. [purchase date] > '1/01/2020' and (t. [sell date] is not null or T. [sell date] > '31/12/2020') THEN '31/12/2020')
When t. [purchase date] > '1/01/2020' and (t. [sell date] is not null or T. [sell date] < '31/12/2020') THEN t. [sell date]
END as [End Date]
FROM Transaction_table t
), cte2 as (
select *
, (select Valuation from Valuation_Table V where V. [Trade Id] =T. [and V. [Valuation Date] = T. [Start Date] ) [估值開始] 。
, (select Valuation from Valuation_Table V where V. [Trade Id] =T. [and V. [Valuation Date] = T. [End Date] ) [Valuation End]。
from cte2 T
)
select *, [Valuation End]- [Valuation Start] 運動
from cte2;
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/332785.html
標籤:
