【问题标题】:tsql - Can i do this in a CASE statement?tsql - 我可以在 CASE 语句中执行此操作吗?
【发布时间】:2013-08-31 14:56:05
【问题描述】:

我有两个结果集,如下所示:

@Rounding

PortfolioID Duration
AAA     -0.1

@FinalOutput

ReportingDate   FundCode    Sector      Rank    Duration    Weight
31/07/2013      AAA         Sector1     1       0           33.5
31/07/2013      AAA         Sector2     2       0.9         29.6
31/07/2013      AAA         Sector3     3       0.6         17.3
31/07/2013      AAA         Sector4     4       0.8         11.8
31/07/2013      AAA         Sector5     5       0.1         3.1
31/07/2013      AAA         Sector6     6       0.1         1.3
31/07/2013      AAA         Sector7     7       0           0.4
31/07/2013      AAA         Sector8     8       -0.9        0
31/07/2013      AAA         Sector9     11      0           -1.3
31/07/2013      AAA         Sector10    100     0           2.8
31/07/2013      AAA         Sector11    101     0           1.5
31/07/2013      AAA         Total       102     1.6         100

我需要做的是从等级 1 和等级 102 的持续时间中减去 @Rounding 中的持续时间,但是如果等级 1 是“Sector1”,那么我需要从等级 2 中减去它。这可能吗在案例陈述中?我已经创建了以下内容,但我想不出如何满足“Sector1”的情况,如果它是排名 1,则永远不会被减去?

    SELECT 
        ReportingDate
    ,   FundCode
    ,   Sector
    ,   [Rank]

    ,   CASE
            WHEN [Rank] IN (1,102) THEN [Duration Contribution] - RD1.Duration
            ELSE [Duration Contribution]

        END     AS [Duration Contribution]

    ,   CASE 
            WHEN [Rank] IN (1,102) THEN Percentage - RD.[Weight]
            ELSE Percentage

        END     AS Percentage

    FROM CTE AS CTE

        INNER JOIN @RoundingDifference AS RD  -- Portfolio Weight Rounding
            ON RD.PortfolioID = CTE.FundCode

        INNER JOIN @RoundingDifferenceDur AS RD1 -- Duration Contribution Rounding
            ON RD1.PortfolioID = CTE.FundCode

    WHERE (Percentage <> 0.0
    OR [Duration Contribution] <> 0.0)

    ORDER BY    ReportingDate
            ,   FundCode
            ,   [Rank]

所以我正在寻找的结果是 Sector 1 Duration 仍然 = 0,但 Sector 2 Duration 和 Total 分别为 1.0 和 1.7。

【问题讨论】:

    标签: sql-server-2008 tsql case common-table-expression


    【解决方案1】:

    在处理集合中的第 N 行时,sql 真的不可能知道它是否在第 N-1 行中找到了特定值。这意味着只有当 Rank 1 记录具有特定的 Sector 值时,您才能真正告诉它对 Rank 2 记录做一些特殊的事情。

    不过,您可以使用子查询来检查集合中是否有 Rank 1 的行以及 Sector1。

    您需要使用详细的案例陈述来执行此操作,如果数据集很大,性能可能会很差。

    select
        case
            when Rank = 102
                then [Duration Contribution] - RD1.Duration
            when Rank = 1 and Sector != 'Sector1'
                then [Duration Contribution] - RD1.Duration
            when Rank = 2 and exists (select * from cte where Rank = 1 and Sector = 'Sector1')
                then [Duration Contribution - RD1.Duration
            else [Duration Contribution]
        end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-24
      • 1970-01-01
      • 2010-10-29
      • 1970-01-01
      • 1970-01-01
      • 2013-07-06
      • 2017-05-07
      • 1970-01-01
      相关资源
      最近更新 更多