【问题标题】:Identify Sequence and Act by it识别序列并按其行事
【发布时间】:2016-12-12 01:15:21
【问题描述】:

我有一张包含多种序列的表格。例如:

ID  G_Ind Amount  rnk_date
1    1    50      1
1    0   100      2 
1    0    50      3 
1    1   100      4
2    0    50      1
2    1   100      2
2    0    50      3
2    1   100      4
3    0    50      1
3    1   100      2
3    1    50      3
3    0   100      4
4    0   100      1
4    1    50      2
4    1   100      3
5    1    50      1
5    1   100      2
5    0    50      3
5    0   100      4
5    1    50      5
5    1   100      6

And my desired output is : 


   ID  G_ind Amount  rnk_date rank_date_internal_forIndG Amount_To_Take  
    1    1    50      1                      1               0            
    1    0   100      2                     NULL            100
    1    0    50      3                     NULL            50
    1    1   100      4                      2              100
    --------------------------------------------------------------------
    2    0    50      1                      NULL           50
    2    1   100      2                       1             0
    2    0    50      3                      NULL           50
    2    1   100      4                       2             100
    -------------------------------------------------------------------
    3    0    50      1                     NULL            50
    3    1   100      2                       1              0
    3    1    50      3                       2             50
    3    0   100      4                     NULL            100
    -------------------------------------------------------------------
    4    0   100      1                     NULL            100
    4    1    50      2                      1              0
    4    1   100      3                      2              100
    -------------------------------------------------------------------
    5    1    50      1                      1              0
    5    1   100      2                      2              0
    5    0    50      3                      NULL           50
    5    0   100      4                      NULL           100
    5    1    50      5                      3              50
    5    1   100      6                      4              100
   --------------------------------------------------------------------

我已经构建了前四列,我正在尝试计算“Amount_To_Take”。

希望解释起来不要太复杂,但我会尝试:

在 Id = 1 中,Amount_To_Take 首次为 0,其中 G_Ind = 1。

*在所有情况下,当 G_Ind 第一次 = 1 时(根据我添加的内部 rank_date - rank_date_internal_forIndG),我们将在 Amount_To_Take 中输入零。

我的问题是当 rank_date_internal_forIndG = 2 时。在 Id - 1,2,3,4 中,我们将对金额求和并将其放在 Amount_To_Take 上,所以很简单:

case when rank_date_internal_forIndG = 2 and G_Ind = 1 then 0 

但如果 ID 为 5,我们不是,因为它不是最后一个。

这些是我目前的所有选择,所以不要费心去想其他的组合。很高兴在这里建议如何处理这个问题(ID = 5 和其他两个四个之间的差异的情况)

【问题讨论】:

    标签: sql sql-server sql-server-2012 case


    【解决方案1】:

    嗯,你可以得到第五列:

    select t.*,
           (case when g_ind = 1
                 then row_number() over (partition by id, g_ind order by rnkdate)
            end) as rank_date_internal_forIndG
    from t;
    

    你可以得到最后一列:

    select t.*,
           (case when rank_date_internal_forIndG = 1 then 0
                 else amount
            end) as amount_to_take
    from (select t.*,
                 (case when g_ind = 1
                       then row_number() over (partition by id, g_ind order by rnkdate)
                  end) as rank_date_internal_forIndG
          from t
         ) t;
    

    【讨论】:

      猜你喜欢
      • 2020-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-25
      • 1970-01-01
      • 2022-12-03
      • 1970-01-01
      • 2020-04-26
      相关资源
      最近更新 更多