I have a table app_order
with columns price
(a number) and side
(side can be either 'up'
or 'down'
).
I need to write a postgreSQL condition within a WHERE
clause that filters app_order
entries for which price
is both:
-
bigger than the average of 2 prices (max price with side=’up’ and min price with side=’down’) – 1 AND
-
smaller than the average of 2 prices (max price with side=’up’ and min price with side=’down’) + 1
How can I nest these conditions to achieve this?
At the end, I need a query similar to this:
SELECT
market AS "MARKET",
SUM(size*price) AS "SUM"
FROM
app_order
WHERE
--Missing condition here
GROUP BY
market
ORDER BY
market ASC
2
Answers
Ended up finding the solution (thanks anyway!):
This is how I understood the question.
Using a CTE (or a subquery; pick the one you prefer), find max "up" and min "down" price. Then join the original
app_order
table with a CTE on conditions you specified.Something like this: