select按月对操作分组SQLRedshift
select opposite operation group by month SQL Redshift
我正在尝试提取每个月不活跃的操作列表:
table 1 "all_opreration" 包含操作 ID 的整个列表
table all_operation
第二个 table“active_operation”包含在指定月份进行的操作
table active operation
所以我想按月获取“非活动操作”(每个月不在 active_operation table
中的操作
==> 希望 table :
wished table inactive operation
我尝试了几种方法都没有成功
提前致谢
您只需要左连接并检查右侧是否为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;
我正在尝试提取每个月不活跃的操作列表:
table 1 "all_opreration" 包含操作 ID 的整个列表 table all_operation
第二个 table“active_operation”包含在指定月份进行的操作
table active operation
所以我想按月获取“非活动操作”(每个月不在 active_operation table
中的操作==> 希望 table :
wished table inactive operation
我尝试了几种方法都没有成功
提前致谢
您只需要左连接并检查右侧是否为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;