本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
NTH_VALUE 範圍函數會相對於視窗的第一列,傳回視窗框之指定列的表達式值。
語法
NTH_VALUE (expr, offset) [ IGNORE NULLS | RESPECT NULLS ] OVER ( [ PARTITION BY window_partition ] [ ORDER BY window_ordering frame_clause ] )
引數
- expr
-
函數運算的目標欄或表達式。
- offset
-
相對於視窗中的第一列,決定要傳回表達式的列號。offset 可以是常數或表達式,且必須為大於 0 的正整數。
- IGNORE NULLS
-
選擇性規格,指出 Amazon Redshift 在決定要使用的列時應該略過 Null 值。如果未列出 IGNORE NULLS,則會包含 Null 值。
- RESPECT NULLS
-
指出 Amazon Redshift 應該包含 null 值來決定要使用的列。如果您不指定 IGNORE NULLS,則預設支援 RESPECT NULLS。
- OVER
-
指定視窗分割、排序及視窗框。
- PARTITION BY window_partition
-
針對 OVER 子句中的每一個群組,設定記錄範圍。
- ORDER BY window_ordering
-
排序每一個分割區內的列。如果省略 ORDER BY,則預設窗框包含分割區中的所有列。
- frame_clause
-
如果彙總函數使用 ORDER BY 子句,則需要明確的窗框子句。窗框子句在排序的結果中包含或排除資料列組,以調整函數視窗中的一個列集。窗框子句包含 ROWS 關鍵字和相關的指定元。請參閱 範圍函數語法摘要。
NTH_VALUE 範圍函數支援有使用任何 Amazon Redshift 資料類型的運算式。傳回類型與 expr 的類型相同。
範例
下列範例顯示加利佛尼亞、佛羅里達及紐約的前三大會場的座位數,並對照這些州其他會場的座位數:
select venuestate, venuename, venueseats,
nth_value(venueseats, 3)
ignore nulls
over(partition by venuestate order by venueseats desc
rows between unbounded preceding and unbounded following)
as third_most_seats
from (select * from venue where venueseats > 0 and
venuestate in('CA', 'FL', 'NY'))
order by venuestate;
venuestate | venuename | venueseats | third_most_seats
------------+--------------------------------+------------+------------------
CA | Qualcomm Stadium | 70561 | 63026
CA | Monster Park | 69843 | 63026
CA | McAfee Coliseum | 63026 | 63026
CA | Dodger Stadium | 56000 | 63026
CA | Angel Stadium of Anaheim | 45050 | 63026
CA | PETCO Park | 42445 | 63026
CA | AT&T Park | 41503 | 63026
CA | Shoreline Amphitheatre | 22000 | 63026
FL | Dolphin Stadium | 74916 | 65647
FL | Jacksonville Municipal Stadium | 73800 | 65647
FL | Raymond James Stadium | 65647 | 65647
FL | Tropicana Field | 36048 | 65647
NY | Ralph Wilson Stadium | 73967 | 20000
NY | Yankee Stadium | 52325 | 20000
NY | Madison Square Garden | 20000 | 20000
(15 rows)