Skip to content
Advertisement

remove results from query if group is null

I have the below table:

Criteria:

Do not show results for grpid if there is a null custuserid for all grpid

grpid 145 should be seen as there is a custuserid on the second row.

output:

Best way to filter out these results?

I was thinking first_value could be used but is there a better way?

Advertisement

Answer

I would simpy use exists:

The correlated subquery ensures that at least one record with the same grpid has a non-null custuserid. For performance, you want an index on (grpid, custuserid).

You could also use window functions:

Or, you can join an aggregate query:

Which option performs best would depend on your dataset (size, cardinality, …).

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