I am querying on Redash to extract an aggregate list of parking places with a pricing grid. The listing table consists of parking details (Name, address, link) and multiple pricing columns depending on duration of stay.
I am using two tables (park and offers). The offers table has three fields, park_id, price and duration. Each park_id may have prices corresponding to a certain duration of stay. As an example, park #1 will charge $2 for 3600 seconds, $30 for 86400 seconds and so on.
I have written separate subqueries to obtain these prices per duration, starting from 1 hour to 24 hours(1 day), then continuing on to 31 days and then for 93 days, 186 days and 372 days. These are 57 subqueries in total.
The resulting table has 57 columns of prices (price1h, price2h….price1d, price 2d….. price31d, price93d, price186d, price372d).
Conditions: There are two important conditions I must use to get the price. One is offers.duration = 3600/7200 etc, and then I filter for offers which package type and not subscription type. Finally, AND offers.park_id = parks.id links the two key fields from the tables park and offers.
The issue is that I am repeating this subquery multiple times for different durations, the execution is painfully slow and exceeds the ‘Redash query execution time limit’
A snippet of the query is below:
SELECT DISTINCT parkid AS ID,
park_name AS Name,
park_address AS full_address,
park_url_description AS park_url,
(SELECT offers.price
FROM offers
WHERE offers.type = 'package'
AND offers.duration = 3600
AND offers.park_id = park.parkid) AS price1h,
(SELECT offers.price
FROM offers
WHERE offers.type = 'package'
AND offers.duration = 7200
AND offers.park_id = park.parkid) AS price2h,
(SELECT offers.price
FROM offers
WHERE offers.type = 'package'
AND offers.duration = 10800
AND offers.park_id = park.parkid) AS price3h,
…….. /* and so on */
FROM park
LEFT JOIN offers ON park.parkid = offers.park_id
GROUP BY id, Name, full_address, park_url
And the result table be something like this: https://i.stack.imgur.com/0opp0.png
Can anyone suggest a better alternative to get these prices? I tried using CTE method but I am not experienced enough so I could not come up with a solution (I'm an intern and still learning.) Thank you in advance.