Hive查詢分組內的最大值行

2022-06-18 05:03:09 字數 644 閱讀 4732

工作中碰到這麼個問題:

現在有**形式如下

日期名字

編碼20200910

a123

20200815

b111

20200625

a234

20200208

b333

with t1 as (

select *,row_number() over (partition by name order by date desc) rn

from table )

select * from t1 where rn=1

開窗後,擴充套件乙個組內排序的字段,然後取出排序第一位的行。

with t1 as(

select * from table order by name,date desc

)select collect_list(date)[0],name,collect_list(code)[0] from t1 group by name

將日期組內排逆序後,轉換成列表(array),取列表內的第乙個元素。感覺寫法上還是沒有第一種方法來得快。

注:以上**未在環境中測試,僅提供思路的偽**

sql多表查詢分組最大值

問題描述 有三張表 學生表student id,name id為主鍵 課程表subject id,name id為主鍵 分數表score studentid,subjectid,score 其中studentid與subjectid為聯合主鍵,一句sql語句查詢出 學號,姓名,課程名,課程最高分.模...

SQL分組最大值

employee表包含所有員工資訊,每個員工有其對應的 id,salary 和 department id。id name salary departmentid 1 joe 70000 1 2 henry 80000 2 3 sam 60000 2 4 max 90000 1 department...

SQL分組求最大值

訂單操作記錄表,需要獲取每個訂單最新的操作更新時間,以及操作id。使用 over 以及 row number 來實現 select from select operationid,orderno,updatetime,row number over partition by orderno orde...