显示每组最大值的配置单元表

时间:2017-12-28 21:29:44

标签: hive greatest-n-per-group hiveql

给定的配置单元查询。

select field_name, sum(frequency) as total_sum, ontology_uri 
from feed_one.dd_instance 
where feed_name="mrna"
group by ontology_uri,field_name order by total_sum desc;

生成下表:

**field_name**          **total_sum**   **ontology_uri**
tcga_participant_barcode    1944    http://data.bioontology.org/ontologies/NCIT
gene                        1251    http://data.bioontology.org/ontologies/GEXO
protocol                    612     http://data.bioontology.org/ontologies/PR
gene                        378     http://data.bioontology.org/ontologies/OGG
geneid                      315     http://data.bioontology.org/ontologies/NCIT
gene                        315     http://data.bioontology.org/ontologies/RETO
cohort                      270     http://data.bioontology.org/ontologies/NCIT

我想使用其结果显示每个field_name的最大total_sum。也就是说,以下是我的期望:

**field_name**          **max(total_sum)**  **ontology_uri**
tcga_participant_barcode    1944    http://data.bioontology.org/ontologies/NCIT
gene                        1251    http://data.bioontology.org/ontologies/GEXO
protocol                    612     http://data.bioontology.org/ontologies/PR
geneid                      315     http://data.bioontology.org/ontologies/NCIT
cohort                      270     http://data.bioontology.org/ontologies/NCIT

我尝试了以下HQL,但是引导我 org.apache.hive.service.cli.HiveSQLException:编译语句时出错:FAILED:ParseException第3行:1输入不匹配'选择'期待(接近'('在表达式规范中

select * from A
where (A.field_name, A.total_sum) IN
(select A.field_name, max(A.total_sum) as maximum
from 
(
select field_name, sum(frequency) as total_sum, ontology_uri 
from feed_one.dd_instance 
where feed_name="mrna"
group by ontology_uri,field_name order by total_sum desc
) as A
group by A.field_name);

1 个答案:

答案 0 :(得分:1)

select t1.field_name, max(t1.total_sum),t1.ontology_uri
from
(
   select field_name, sum(frequency) as total_sum, ontology_uri 
   from feed_one.dd_instance 
   where feed_name="mrna"
   group by ontology_uri,field_name
) t1
group by t1.field_name, t1.ontology_uri

使用row_number()partition

select * from 
(
   select 
        field_name,
        ontology_uri,
        sum(frequency) over (partition by field_name,ontology_uri) as total_sum,
        row_number() over (partition by field_name,ontology_uri order by total_sum desc) as row_no
   from feed_one.dd_instance 
   where feed_name="mrna"
) res 
where res.row_no = 1