Sql 在配置单元中创建新表,根据唯一条件选择记录

Sql 在配置单元中创建新表,根据唯一条件选择记录,sql,hive,hql,Sql,Hive,Hql,我需要在配置单元中创建数据表,该表将包含具有2个或更多记录的ID,这些记录具有100天或更长的时间,我如何在配置单元中做到这一点? 您可以使用窗口函数计算天数等于或大于100天的行数: select t.* from (select t.*, sum(case when days >= 100 then 1 else 0 end) over (partition by id) as cnt_100pl from t ) t where cnt

我需要在配置单元中创建数据表,该表将包含具有2个或更多记录的ID,这些记录具有100天或更长的时间,我如何在配置单元中做到这一点?

您可以使用窗口函数计算天数等于或大于100天的行数:

select t.*
from (select t.*,
             sum(case when days >= 100 then 1 else 0 end) over (partition by id) as cnt_100pl
      from t
     ) t
where cnt_100pl >= 2;

您可以使用Gordon在其回答中提出的窗口函数。您还可以使用如下所示的相关子查询来完成此操作。(假设表名为my_表)

选择t1*
从我的表t1
其中2=100);
因此,完整的查询将是

Create table my_target_table
As
Select t1.*
    from my_table t1 
    where 2 <= (Select count(1) from my_table t2 where t2.id = t1.id and t2.days >= 100); 
创建表格我的目标表格
像
选择t1*
从我的表t1
其中2=100);
Create table my_target_table
As
Select t1.*
    from my_table t1 
    where 2 <= (Select count(1) from my_table t2 where t2.id = t1.id and t2.days >= 100);