How would I grab the current lowest price for a Game
given this query I’m using for my search results.
For each Game, I need to grab every StoreItem
. And for every StoreItem
grab the latest by StoreItemPrice.inserted_at
.
These are the current prices across multiple stores.
Finally return the StoreItemPrice that has the lowest StoreItemPrice.price
value.
That’s the Game’s current lowest price.
query =
from(g in Game,
order_by: [desc_nulls_last: :first_release_date],
where:
fragment(
"to_tsvector('english', name || ' ' || slug) @@ to_tsquery(?)",
^prefix_search(search_term)
),
preload: [
:genres,
:platforms,
store_items: [:store_item_prices]
]
)
Something like this feels about right but not quite there. Since it’s grabbing the absolute lowest StoreItemPrice
and that price could be old and superseded by a newer higher price in that store.
query =
from(g in Game,
order_by: [desc_nulls_last: :first_release_date],
where:
fragment(
"to_tsvector('english', name || ' ' || slug) @@ to_tsquery(?)",
^prefix_search(search_term)
),
preload: [
:genres,
:platforms,
store_items: [
store_item_prices: [
order_by: [asc: :price],
limit: 1
]
]
]
)
I need the latest price per store, then use the lowest price of between those stores.
Been noodling on this for a day or so, and kind of stuck.