Skip to content
Advertisement

how i can make left join work only when the number of records in the first table more or equal the number of records in the second table?

I have two tables and I want do left join between them, but I want the left join to happen only if the count of records that contain the same value of join column in the first table is more or equal the count of records that contain the same value of join column in the second table

what I try:

first I do the count of records per the each group

and then I put condition to filter (a.cnt >= b.cnt)

this is the code:

But my try not work because the join done first and then the condition (so the value of cnt in the second table will not stay the same after join done)

Any idea how to make that work?

Advertisement

Answer

You want to write your query referencing a neutral “seed table” in your FROM clause. Then you can calculate the row counts from the two tables and compare them to each other. Something like this:

I’m not sure how about the seqnum logic, so you can add it back in if needed.

I haven’t tested it and you may need to tinker with the syntax to get it to work with Presto. Give it a shot and let me know.

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