Skip to content
Advertisement

Group by calculated field within multiple inner joins / subselects

I’m trying to find stories that are the closest distance from the user. However, only chapters in our stories have a place, so I need to get those items and then get the closest chapter from that list, then sort the stories via that distance that has been retrieved. The hardcoded numbers here are just for testing/example and would be dynamically replaced with the user’s location at query time.

I’ve created a function that gets me pretty close, but it has multiple entries for each chapter instead of just one for each story using the minimum, distance value from the chapters.

I have a query that gives me the minimum chapter distance out of all the chapters:

This works as expected.

I played with a subselect version of this that says distance isn’t defined:

Is there a way to combine or use these together to just get me each story sorted by their distance?

Advertisement

Answer

You can try this :

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement