使用 select 语句中创建的基于条件的列连接两个表

Join two tables with conditions based columns created in select statement

问题来了。我有两个 tables

Transaction_Table

TradeID Purchase Date Sell Date
1234T 12/04/2002
1235T 11/05/2020 30/08/2020
1236T 15/07/2010 17/01/2020
1237T 19/12/2020

Valuation_Table(table 包含所有日期和所有交易的估值)

Trade ID Valuation Date Valuation
1234T 01/01/2020 £100
1234T 31/12/2020 £200

我正在尝试根据与财政年度(例如 2020 年 1 月 1 日至 2020 年 12 月 31 日)相关的买卖时间来计算交易的估值变动 规则如下:

  1. 交易在财政年度开始前购买,在结束前不出售 --> 移动计算为 1/01/2020-31/12/2020
  2. 交易是在财政年度内买卖的 --> 按购买日期 - 销售日期计算的变动
  3. 交易是在财政年度购买的,但没有出售 --> 购买日期 - 2020 年 12 月 31 日
  4. 交易在财政年度之前购买并在财政年度出售 --> 1/01/2020 - 出售日期

我在 select 语句中使用 CASE 向交易 table 添加了估值开始和结束日期,就像这样。

    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.purchase date < 1/01/2020 and t.sell date < 31/12/2020 THEN t.sell date
    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
    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 < 31/12/2020 THEN t.sell date
    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

这将创建以下 table

Trade ID Purchase Date Sell Date Start Date End Date
1234T 14/04/2002 1/01/2020 31/12/2020
1235T 11/05/2020 30/08/2020 11/05/2020 30/08/2020
1236T 15/07/2010 17/01/2020 01/01/2020 17/01/2020
1237T 19/12/2020 19/12/2020 31/12/2020

我现在正在努力的是根据 select 语句中创建的开始和结束日期添加来自估值 table 的估值。问题似乎是这些列仅通过 select 语句存在,而在原始事务 table.

中不存在

最终结果应该是这样的

Trade ID Purchase Date Sell Date Start Date End Date Valuation Start Valuation End Movement
1234T 14/04/2002 1/01/2020 31/12/2020 £100 £200 +100

如果您需要使用计算列的结果,只需使用派生的 table 子查询(我使用的 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.[purchase date] < '1/01/2020' and t.[sell date] < '31/12/2020' THEN t.[sell date]
        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
        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] < '31/12/2020' THEN t.[sell date]
        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.[Trade Id] and V.[Valuation Date] = T.[Start Date]) [Valuation Start]
        , (select Valuation from Valuation_Table V where V.[Trade Id] = T.[Trade Id] and V.[Valuation Date] = T.[End Date]) [Valuation End]
    from cte2 T
)
select *, [Valuation End]- [Valuation Start] Movement
from cte2;