Skip to content

Left Join Lateral logic for Query() #331

Answered by stephenafamo
VRmnv asked this question in Q&A
Discussion options

You must be logged in to vote

As mentioned in the documentation, ThenLoadXXXXX does a separate query. So you should add your additional mods with this in mind.

To illustrate, your current query will run 2 queries:

SELECT * 
FROM sources;

-- Assuming the foreign key column is source_id
SELECT * 
FROM iterations 
WHERE iterations.source_id IN (....)
LIMIT 3; -- this is the mod you added.

What you want to do needs a slightly more complicated 2nd query:

WITH selected AS (
    SELECT id, rank() OVER (PARTITION BY source_id ORDER BY id) as "rank"
    FROM iterations
)
SELECT *
FROM iterations
INNER JOIN selected ON selected.id = iterations.id AND selected.rank <= 3 -- this 3 or less of each source_id
WHERE iterations.sourc…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@VRmnv
Comment options

@stephenafamo
Comment options

@VRmnv
Comment options

Answer selected by stephenafamo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #330 on December 26, 2024 12:11.