I would like to refactor my query to use a subquery as a function argument.
The original query looks like this and works fine:
SELECT date_trunc('hour', (SELECT timestamp FROM data ORDER BY timestamp DESC LIMIT 1));
I wonder if it's possible to refactor a query to pass an aliased value as an argument instead of the above shown SELECT statement.
I tried to come up with something like this without luck:
WITH ts_query AS (
SELECT "timestamp" FROM "data" ORDER BY "timestamp" DESC LIMIT 1
)
SELECT date_trunc('hour', ts_query);
As I get the following error:
ERROR: column "ts_query" does not exist