【问题标题】:Creating a custom column with different group names for same row element in redshift为redshift中的同一行元素创建具有不同组名的自定义列
【发布时间】:2021-03-22 15:48:06
【问题描述】:

我的表在 redshift 中看起来像这样

id name value
1 element1 100
2 element2 200
3 element3 100
4 element4 50
1 element1 100
2 element2 200
5 element5 250

我正在寻找动态创建一个名为组的自定义列,例如 group1 将包含 id (1,2,4) 而 group2 将是 (1,2,5) 并且预期的输出是这样的。预期输出将是

cus col value
group1 650
group2 850
3 100

table sample

我用 case 语句尝试了下面的查询,但 group2 没有添加元素 1 和 2,只添加到 group1 中

当 id 在 (1,2,4) 时选择 case 然后 'group1 当 id 在 (1,2,5) 中时 'group2' 否则 id 结束, 样本的总和(值) 按 1 分组

【问题讨论】:

    标签: sql grouping amazon-redshift rollup


    【解决方案1】:
    --drop table element_data;
    create table element_data(id number(10),name varchar2(10),value number(10));
    
    insert into element_data values(1,'e1',100);
    insert into element_data values(2,'e2',200);
    insert into element_data values(3,'e3',100);
    insert into element_data values(4,'e4',50);
    insert into element_data values(1,'e1',100);
    insert into element_data values(2,'e2',200);
    insert into element_data values(5,'e5',250);
    select * from element_data;
    
    
    select sum(output) from(
    select  unique ID,(case when id=1 or id =5 or id=2 then
                  sum(value) over(partition by name ) end) output from element_data)
                  UNION all
                  select sum(output) from(
    select  unique ID,(case when id=1 or id =2 or id=4 then
                  sum(value) over(partition by name ) end) output from element_data)
                  union all
                  select sum(output) from(
    select  unique ID,(case when id=3 then
                  sum(value) over(partition by name ) end) output from element_data)
                  ;
    

    【讨论】:

      【解决方案2】:
      WITH cte AS (
      SELECT
      case 
      when id in (1,2,4) then 'group1' 
      when id in (1,2,5) then 'group2' 
      else 'other' end as mygroup,
      value
      FROM yourTable
      )
      SELECT mygroup,
      sum(value) over (partition by position mygroup order by mygroup) as totalValue
      FROM cte
      group by mygroup
      

      【讨论】:

      • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
      【解决方案3】:

      这有点棘手,因为这些组是重叠的。我建议:

      with group_mapping as (
            select 1 as id, 'group1' as grp union all
            select 2 as id, 'group1' as grp union all
            select 4 as id, 'group1' as grp union all
            select 1 as id, 'group2' as grp union all
            select 2 as id, 'group2' as grp union all
            select 5 as id, 'group3' as grp
           )
      select coalesce(gm.grp, t.id::text) as grp, sum(value)
      from t left join
           group_mapping gm
           on gm.id = t.id
      group by coalesce(gm.grp, t.id::text);
      

      注意:如果您有一个包含组映射信息的表,则不需要 CTE。

      如果组不重叠,则另一种方法是通过 case 表达式进行聚合。

      【讨论】:

        猜你喜欢
        • 2017-05-25
        • 2011-09-07
        • 1970-01-01
        • 2013-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-24
        • 1970-01-01
        相关资源
        最近更新 更多