Skip to content
Advertisement

Presto SQL save query results in variable

I have a database that I am querying with athena.

I am using subqueries to select a subset of the data like so

can I save the query results of

in a variable VAR so that we need not query it again and again and also to make query look cleaner?

Advertisement

Answer

There is no such concept as variable in Presto. You can using the query results from S3 as the source for the new table but I would argue that it can be brittle and cumbersome. As for “reusing” the code – you can achieve that with WITH clause designed especially for that:

Another approach which can be worth looking at is using grouping sets (see corresponding point of “Complex Grouping Operations” in the GROUP BY Clause docs):

Output

item date max_date max_date2
id1 1 1 2
id1 2 2 1
id1 2 2
id2 1 1 1
id2 1 1

date will be equal to max_date for rows which are grouped both by item and date

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement