1

I have a table with columns name and full name, and am interested in knowing whether one is part of the other (but they are not equal).
Example:

name        |  full name      |  is_subset
-------------------------------------------
john smith  |  john smith     |  false
john smith  |  john h. smith  |  true
john smith  |  alice jones    |  false

I found a method that works using SPLIT and array intersections, but was curious as to whether there is a simpler or more efficient method.

SELECT 
name,
full name,
CARDINALITY(
    (
        ARRAY_INTERSECT(
            SPLIT(name, ' '),
            SPLIT(full name, ' ')
        )
    )
) = CARDINALITY(SPLIT(name, ' ')) AND CARDINALITY(
    SPLIT(full name, ' ')
) > CARDINALITY(SPLIT(name, ' ')) AS is_subset
from t
6
  • Pseudo code : (name EXCEPT fullname) = [] AND (fullname EXCEPT name) != [] Commented May 15, 2024 at 6:38
  • Do you care about duplicates? Such as a b being compared to a a b? Commented May 15, 2024 at 6:52
  • Also, should a b be considered a subset of b a, as they're not equal? Commented May 15, 2024 at 7:04
  • @MatBailie no, don't care about duplicates, a b is equal to a a b. And similarly, since order doesn't count, a b equals b a. Commented May 15, 2024 at 8:13
  • 1
    Using ARRAY_EXCEPT and comparisons to empty arrays, should be enough then. Commented May 15, 2024 at 9:48

1 Answer 1

2
-- name is a logical subset of fullname
ARRAY_EXCEPT(
   SPLIT(name, ' '),
   SPLIT(fullname, ' ')
)
=
ARRAY []

AND

-- name and fullname are unequal sets
ARRAY_EXCEPT(
   SPLIT(fullname, ' '),
   SPLIT(name, ' ')
)
!=
ARRAY []

The second condition can probably be reduced to...

AND

LENGTH(name) < LENGTH(fullname)

(The shorter LENGTH() version can be "tricked" by certain uses of duplicates.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.