【问题标题】:Create columns where sum other columns based on conditional of other column values?根据其他列值的条件创建对其他列求和的列?
【发布时间】:2020-09-17 12:55:12
【问题描述】:

我需要创建列,其中根据其他列值的条件对其他列求和。要求如下。

我有下表:

key code1  code2 code3 code4 value1 value2 value3 value4 
0    101   101   101   101   1000    1000  1000   1000    
1    101   101   101   201   1000    1000  1000   1000    
2    101   101   201   201   1000    1000  1000   1000    
3    101   201   201   201   1000    1000  1000   1000    
4    101   201   201   301   1000    1000  1000   1000    
5    101   201   301   301   1000    1000  1000   1000    
6    101   301   301   301   1000    1000  1000   1000    
7    101   101   101   301   1000    1000  1000   1000    
8    101   201   301   0     1000    1000  1000   0       
9    101   301   0     0     1000    1000  0      0       
....

考虑到列代码(code1、code2、code3、code4),我需要创建一列来对列值(value1、value2、value3、value4)求和。结果应该是这样的:

key  code1 code2 code3 code4 value1 value2 value3 value4 sum_code_101 sum_code_201 sum_code_301
0    101   101   101   101   1000    1000  1000   1000     4000           0           0
1    101   101   101   201   1000    1000  1000   1000     3000           1000        0
2    101   101   201   201   1000    1000  1000   1000     2000           2000        0
3    101   201   201   201   1000    1000  1000   1000     1000           3000        0
4    101   201   201   301   1000    1000  1000   1000     1000           2000        1000
5    101   201   301   301   1000    1000  1000   1000     1000           1000        2000
6    101   301   301   301   1000    1000  1000   1000     1000           0           3000
7    101   101   101   301   1000    1000  1000   1000     3000           0           1000
8    101   201   301   0     1000    1000  1000   0        1000           1000        1000
9    101   301   0     0     1000    1000  0      0        1000           0           1000  

由于真实表有 25 个不同的代码(101、201、301...),我需要创建 25 列来汇总它们的值。

任何帮助将不胜感激。

【问题讨论】:

    标签: sql sql-server tsql sql-server-2012 unpivot


    【解决方案1】:

    您可以使用case 表达式:

    select
        t.*,
        case when code1 = 101 then value1 else 0 end
            + case when code2 = 101 then value2 else 0 end
            + case when code3 = 101 then value3 else 0 end
            + case when code4 = 101 then value4 else 0 end
            as sum_code_101,
        case when code1 = 201 then value1 else 0 end
            + case when code2 = 201 then value2 else 0 end
            + case when code3 = 201 then value3 else 0 end
            + case when code4 = 201 then value4 else 0 end
            as sum_code_201,
        case when code1 = 301 then value1 else 0 end
            + case when code2 = 301 then value2 else 0 end
            + case when code3 = 301 then value3 else 0 end
            + case when code4 = 301 then value4 else 0 end
            as sum_code_301
    from mytable t
    

    您可以通过取消旋转cross apply 的行来稍微缩短语法,然后使用条件聚合:

    select
        t.*,
        s.*
    from mytable t
    outer apply (
        select 
            sum(case when code = 101 then val else 0 end) sum_code_101,
            sum(case when code = 201 then val else 0 end) sum_code_201,
            sum(case when code = 301 then val else 0 end) sum_code_301,
            sum(case when code = 401 then val else 0 end) sum_code_401
        from (values 
            (t.code1, t.value1), 
            (t.code2, t.value2), 
            (t.code3, t.value3), 
            (t.code4, t.value4)
        ) as x(code, val)
    )  s
    

    【讨论】:

    • 感谢您的帮助!由于真实表有 25 个不同的代码(101、201、301...),有没有办法自动完成?
    • @M_F:你想要列中的结果,所以在某些时候你需要枚举它们。尽管如此,我还是用一个使用cross apply 的解决方案更新了我的答案,这会稍微缩短查询时间。
    猜你喜欢
    • 2020-05-30
    • 2020-09-24
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 2014-05-07
    • 2023-03-17
    相关资源
    最近更新 更多