如何使用sql查找每组重复行的顶行?

时间:2017-09-27 00:29:10

标签: sql

例如,对于给定的表格。

i  |  j   | v  |
---|------|----|
1  |  2   | 2  |
1  |  2   | 3  |
1  |  2   | 4  |
1  |  4   | 1  |
1  |  4   | 6  |
1  |  4   | 8  |

3 个答案:

答案 0 :(得分:0)

从我假设对于FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.mr.MapredLocalTaski列中的每个重复值集合的问题,您试图从jminimum值中检索{ {1}}。

获取maximum行:

v

<强>结果:

max

要获取v的SELECT t1.i ,t1.j ,max(t1.v) AS v FROM table1 t1 GROUP BY t1.i ,t1.j ORDER BY t1.i ,t1.j 值行,请在上述查询中将i | j | v --------- 1 2 4 1 4 8 替换为min

<强> DEMO

答案 1 :(得分:0)

尝试使用以下短代码:

select i, j, MAX(v) v from #temp group by i, j

<强>结果:

i | j | v
---------
1   2   4
1   4   8

答案 2 :(得分:0)

create table #temp (i int, j int , k int)

insert into #temp values(1,2,2)
insert into #temp values(1,2,3)
insert into #temp values(1,2,4)
insert into #temp values(1,4,1)
insert into #temp values(1,4,6)
insert into #temp values(1,4,8)

SELECT * from #temp;

WITH CTE  as
(select *, ROW_NUMBER () over (partition by j order by k)  rowno from #temp )
select i,j,k from cte where rowno = 1