Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 选择配置单元中分区的最后一行_Sql_Hive_Hiveql - Fatal编程技术网

Sql 选择配置单元中分区的最后一行

Sql 选择配置单元中分区的最后一行,sql,hive,hiveql,Sql,Hive,Hiveql,我有一张t1表: c1 | c2 | c3| c4 1 1 1 A 1 1 2 B 1 1 3 C 1 1 4 D 1 1 4 E 1 1 4 F 2 2 1 A 2 2 2 A 2 2 3 A 我想选择每个c1、c2对的最后一行。在这种情况下,1,1,4,F和2,2,3,A。我的想法是这样做: create table t2 as select *,

我有一张t1表:

c1 | c2 | c3| c4
1    1    1   A
1    1    2   B
1    1    3   C
1    1    4   D
1    1    4   E
1    1    4   F
2    2    1   A
2    2    2   A
2    2    3   A
我想选择每个c1、c2对的最后一行。在这种情况下,1,1,4,F和2,2,3,A。我的想法是这样做:

create table t2 as
select *, row_number() over (partition by c1, c2 order by c3) as rank
from t1

create table t3 as
select a.c1, a.c2, a.c3, a.c4
from t2 a
inner join
(select c1, c2, max(rank) as maxrank
 from t2
 group by c1, c2
 )
on a.c1=b.c1 and a.c2=b.c1
where a.rank=b.maxrank 
这样行吗?有环境问题,所以无法测试自己

只需使用子查询:

select t1.*
from (select t1.*, row_number() over (partition by c1, c2 order by c3 desc) as rank
      from t1 
     ) t1
where rank = 1;

请注意,order by使用desc。

mysql没有行号函数。编辑以删除mysql标记,对此表示抱歉。没有关于hiveql的任何线索,但我想您应该能够修改行号,使其按降序排列。然后你可以选择rank=1的行。我觉得c4 desc的顺序也是必需的。但是既然我们有相同的c3,那不是选择1,1,4,D行吗?此外,c4列不一定是有序的@vkp@Danzo . . . 此查询任意选择其中一个具有最大c3值的行。这个问题似乎无法解释这意味着什么。您的代码仅使用c3进行比较。如果c4是比较的一部分,只需使用按c3描述的顺序,c4描述,但我不确定这是您的意图。尝试以下操作:从select*中选择t1.*,按c1对分区排序,按c3描述的c2顺序,从t1中选择c4描述,其中秩=1;