統計量:count,sum,avg,max/min,median,stddev,stddev_samp
排名:row_unmber,rank,dense_rank,percent_rank
其他類:lag,lead,cluster_sample
--------------------
基本用法;把數據按照一定條件分成多組稱爲開窗,每個組稱爲一個窗口
partition by部分用來指定開窗的列
分區列的值相同的行被視爲在同一個窗口內
order by用來指定數據在一個窗口內如何排序
使用限制:只能出現在select子句中
窗口函數中不要嵌套使用窗口函數和聚合函數
不可以和同級別的聚合函數一起使用
一個odps sql語句中,可以使用至多5個窗口函數
Partition開窗時,同一窗口內最多包含1億行數據
用rows開窗時,x,y必須大於等於0的整數常量,限定範圍0-10000,值爲0時表示當前行
必須使用order by纔可以用rows方式指定窗口範圍
並非所有的窗口函數都可以用rows指定開窗方式,支持這種用法的窗口函數有avg,count,max,min,stddev和sum
----------------------
舉個栗子
select *,rank() over(partition by monitor_id order by distance) as mindistance_monitor_id from()
select *,rank() over(partition by monitor_id order by distance) as mindistance_monitor_id from()
算地圖經緯度距離:
sqrt(pow((lng-jiaojin_lng)*111000,2)+pow((lat-jiaojin_lat)*111000*cos(30),2)) as distance
時間開窗:
select *,row_number() over(partition by id_card order by info_enabled_time desc) as cnt from ods_prsn_census_person_info_gaj_m
where dt='${today}' and write_off_sign_no='0' and person_status_no='0'
ROW_NUMBER()排序和rank()區別,後者並列排序
字段拆分成列函數:
select cast(rid as string) as rid,
adcode,
wm_concat(';',concat(rdseg_id,',',rdseg_name,',',rdseg_len)) as rdseg_info_list
from
${dwd_tfc_bas_rdnet_rdseg_info}
LATERAL VIEW explode(SPLIT(ridseq,',')) middle_table as rid
where
data_version = '${data_version}'
and adcode = '${adcode}'
group by
cast(rid as string),
adcode