How to re-use the result of a subquery in the main query, with Postgres

Thomas

Here is a crude example of the problem I'm trying to solve:

SELECT
    (array_agg(ts))[1] as ts,
    (array_agg(ticker))[1] as ticker,
    round(avg(m1)::decimal,  (SELECT price_tick_size FROM exchange_debug.instruments WHERE ticker = 'BTCUSDT')) as m1,
    round(avg(m5)::decimal,  (SELECT price_tick_size FROM exchange_debug.instruments WHERE ticker = 'BTCUSDT')) as m5,
    round(avg(m15)::decimal, (SELECT price_tick_size FROM exchange_debug.instruments WHERE ticker = 'BTCUSDT')) as m15,
    round(avg(m30)::decimal, (SELECT price_tick_size FROM exchange_debug.instruments WHERE ticker = 'BTCUSDT')) as m30,
    ...

I need to know the rounding value for the data, load it once and reuse it everywhere.

I thought about using 'USING' above the query to get the rounding value:

WITH tick_size AS
    (SELECT price_tick_size FROM exchange_debug.instruments WHERE ticker = 'BTCUSDT')
SELECT
    (array_agg(ts))[1] as ts,
    (array_agg(ticker))[1] as ticker,
    round(avg(m1)::decimal, tick_size.price_tick_size),
    round(avg(m5)::decimal, tick_size.price_tick_size),
    ...

But then I get the following error:

missing FROM-clause entry for table "tick_size"

So, I can do this:

round(avg(m1)::decimal, (SELECT price_tick_size FROM tick_size)) as m1,

But I can't help to think there has to be something simpler.

Bergi

You can put the subquery in the FROM clause of your main query, as long as it is guaranteed to return exactly a single row:

SELECT
    (array_agg(ts))[1] as ts,
    (array_agg(ticker))[1] as ticker,
    round(avg(m1)::decimal, tick_size.price_tick_size),
    round(avg(m5)::decimal, tick_size.price_tick_size),
    ...
FROM
    ...,
    (SELECT price_tick_size FROM exchange_debug.instruments WHERE ticker = 'BTCUSDT') AS tick_size

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to use column from main query in subquery?

How to append the output of subquery to the current main query?

php mysql use subquery result in WHERE clause in the same query

How to use the result of a subquery

SQL, use the result of main query into a sub query

Use result of one query to query another in Postgres

Subquery count() for postgres query

How to use postgres function result as a separate column in DELETE query

How to use left function in subquery using Postgres

Stringify subquery results into main query

Is it possible to use the result of a subquery in a case statement of the same outer query?

How can I use a subquery on my query results and then ORDER BY a calculated result for each row?

Postgres: How to use value of field in subquery

How can I add to select multiple values of subquery in main query

How to use insert query where one of the value is a result of a subquery

sql server: looping through a subquery and limting result in main query corresponding to evry row in subquery

MySQL subquery or join different databases where database name is in the main query's result set

Mysql - use result of another query inside subquery

How i can use result of subquery in this query?

How to link Ebean subquery to main query

Main query results use again in Subquery

Sort main query by subquery

Subquery based on column in main query

How to find rows where subquery string contains main query string?

how can I bring value to main query from subquery

Update a table use result of another query in Postgres

SQL Server - Query - How to get the last result of a subquery?

How to move variables from subquery to an outer query - postgres?

How to use postgres subquery result to filter overall results