Flink常见SQL语法说明
SELECT与WHERE语句
根据where子句对数据进行过滤。
- 示例:
SELECT price + tax FROM Orders WHERE id = 10
WITH语句
WITH子句提供了一种用于更大查询而编写辅助语句的方法。这些语句通常被称为公用表表达式,表达式可以理解为仅针对某个查询而存在的临时视图。
窗口聚合语句
通过窗口聚合进行分组的查询将计算每个组的单个结果行。
窗口去重语句
Window Deduplication是一种特殊的重复数据删除,它删除在一组列上重复的行,为每个窗口和分区键保留第一个或最后一个。
- 语法:
FROM (
SELECT [column_list],
ROW_NUMBER() OVER (PARTITION BY window_start, window_end [, col_key1...]
ORDER BY time_attr [asc|desc]) AS rownum
FROM table_name) -- relation applied windowing TVF
WHERE (rownum = 1 | rownum <=1 | rownum < 2) [AND conditions]
- 示例:
SELECT * FROM ( SELECT bidtime, price, item, supplier_id, window_start, window_end, ROW_NUMBER() OVER (PARTITION BY window_start, window_end ORDER BY bidtime DESC) AS rownum FROM TABLE( TUMBLE(TABLE Bid, DESCRIPTOR(bidtime), INTERVAL '10' MINUTES)) ) WHERE rownum <= 1;
Top-N语句
Top-N查询要求按列排序的N个最小值或最大值。
- 语法:
FROM (
SELECT [column_list],
ROW_NUMBER() OVER ([PARTITION BY col1[, col2...]]
ORDER BY col1 [asc|desc][, col2 [asc|desc]...]) AS rownum
FROM table_name)
WHERE rownum <= N [AND conditions]
- 示例:
CREATE TABLE ShopSales ( product_id STRING, category STRING, product_name STRING, sales BIGINT ) WITH (...); SELECT * FROM ( SELECT *, ROW_NUMBER() OVER (PARTITION BY category ORDER BY sales DESC) AS row_num FROM ShopSales) WHERE row_num <= 5