Left Join Lateral logic for Query() #331
-
Hello! I have this sql query and i want to rewrite it into bob. SELECT source.uuid,
iteration.uuid
FROM source
LEFT JOIN LATERAL (
SELECT *
FROM iteration i WHERE i.source_uuid = source.uuid
ORDER BY created_at DESC
LIMIT 3) iteration ON true I don't know how to implement this behavior using Query(). My Query() looks like this: sourcesWithIterations, err := models.Sources.Query(
models.ThenLoadSourceIterations(
sm.Limit(*iterationsLimit),
),
).All(ctx, s.db) However, i'm getting only first 3 iterations, not first 3 iterations for each source. Is there a common way to get my desired behavior using Query() or i should build query using builder? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
As mentioned in the documentation, 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.source_id IN (....); Knowing this, we can now attempt to craft in Bob: models.Sources.Query(
models.ThenLoadSourceIterations(
sm.With("selected").As(
models.Iterations.Query(
sm.Select(models.IterationColumns.ID),
psql.F("rank")(fm.Over().
PartitionBy(models.IterationColumns.SourceID).
OrderBy(models.IterationColumns.ID)), // Change the order as you like
),
),
sm.InnerJoin("selected").On(
psql.Quote("selected", "id").EQ(models.SourceColumns.ID),
psql.Quote("selected", "rank").LTE(psql.Raw(*iterationsLimit)),
),
),
) Try this and let me know if it works. |
Beta Was this translation helpful? Give feedback.
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:
What you want to do needs a slightly more complicated 2nd query: