【问题标题】:select opposite operation group by month SQL Redshift按月选择对面操作组 SQL Redshift
【发布时间】:2021-12-07 18:40:45
【问题描述】:

我正在尝试提取每个月不活动的操作列表:

表 1“all_opreration”包含整个操作 ID 列表 table all_operation

第二个表“active_operation”包含在指定月份处于活动状态的操作

table active operation

所以我想按月获得“非活动操作”(对于每个月不在 active_operation 表中的操作

==> 心愿表:

wished table inactive operation

我尝试了几种方法,但都没有成功

提前谢谢你

【问题讨论】:

    标签: amazon-redshift


    【解决方案1】:

    您只需要左连接并检查右侧是否为NULL。您将需要扩展 all_operations 以包含所有日期,但这可以通过交叉连接来完成。像这样:

    设置:

    create table all_ops (op_id varchar(32));
    insert into all_ops values ('A'), ('B'), ('C');
    
    create table active_ops (month date, op_id varchar(32));
    insert into active_ops values ('2021-10-01', 'A'),
    ('2021-10-01', 'B'),
    ('2021-07-01', 'C');
    

    查找缺失的数据/id 对:

    select l.month, l.op_id 
    from (
      select month, op_id
      from all_ops 
      cross join (select distinct month from active_ops) m
      ) l
    left join active_ops r
    on l.op_id = r.op_id and l.month = r.month
    where r.op_id is null;
    

    【讨论】:

    • 工作正常,非常感谢比尔
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多