Amazon Athena - ANSI SQL - Use inner or nested SELECT statements as alternative to temp tables or variables
Amazon Athena - ANSI SQL - Use inner or nested SELECT statements as alternative to temp tables or variables
When creating more complex SQL queries, it can help to break the problem down into smaller parts.
With other platforms such as Microsoft SQL Server (T-SQL) it is possible to use temp tables, or variables, to stage the retrieval work.
However Amazon Athena uses the more limited ANSI flavour of SQL.
One option to at least break down the development side of the problem, is to use nested or inner SELECT statements.
Example:
Here, the innermost SELECT prepares the columns, processing a timestamp into a simpler text format (year and month as a VARCHAR). Relevant property 'run_kind' is extracted from a JSON field.
Then, the outer SELECT statements do not need to deal with these extraction steps, and can focus on aggregation and ordering.
So there is a kind of separation of responsibility between the inner select statement, that extracts and prepares fields, and then out select statements that perform aggregation and final ordering.
note: this needs careful construction to avoid very expensive queries, but it is a technique that allows for sophisticated querying and aggregation, even in ANSI SQL.
Comments
Post a Comment