如何 select 来自 impala sql 列的两个最大日期

how to select the two largest dates from a column where impala sql

我有以下专栏

 extract_dt
------------
2022-02-06 |
2022-02-06 |
2022-02-06 |
2022-02-06 |
2022-02-06 |
2022-02-06 |
2022-02-06 |
2022-01-30 |
2022-01-30 |
2022-01-30 |
2022-01-30 |
2022-01-23 |
2022-01-23 |
2022-01-23 |
2022-01-23 |
2022-01-23 |
2022-01-23 |

我只想自动获取具有最近两个日期的数据,因为每周都会执行新的提取并在列中输入新日期。

我尝试使用它,但没有成功。

SELECT distinct top(2) extract_dt
FROM table
ORDER BY extract_dt desc

并得到以下错误

AnalysisException: Syntax error in line 263: where extract_dt = SELECT distinct top(2) extract_dt ^ Encountered: SELECT Expected: CASE, CAST, EXISTS, FALSE, IF, INTERVAL, NOT, NULL, TRUNCATE, TRUE, IDENTIFIER CAUSED BY: Exception: syntax error

我也试过了

SELECT MAX(extract_dt)
FROM table
WHERE extract_dt NOT IN (SELECT Max (extract_dt)
                           FROM table);

返回以下错误

AnalysisException: Syntax error in line 263: SELECT MAX(extract_dt) ^ Encountered: SELECT Expected: AND, AS, ASC, BETWEEN, CROSS, DESC, DIV, ELSE, END, FOLLOWING, FROM, FULL, GROUP, IGNORE, HAVING, ILIKE , IN, INNER, IREGEXP, IS, JOIN, LEFT, LIKE, LIMIT, LOCATION, NOT, NULLS, OFFSET, ON, OR, ORDER, PRECEDING, RANGE, REGEXP, RIGHT, RLIKE, ROWS, SET, STRAIGHT_JOIN, THEN, UNION , USING, WHEN, WHERE, COMMA, IDENTIFIER CAUSED BY: Exception: Syntax error

您可以使用 limit:

select distinct extract_dt
from table
order by extract_dt desc
limit 2

另一种解决方案可以是 User-defined 函数。第一轮,你将select

SELECT Max (extract_dt) 
FROM table INTO variable

然后 SELECT 并与另一个 SELECT 中的值进行比较。