0

I'm trying to select rows from a table where some value a is within c columns at least b times.

Here is a simplified example of the data I'm working with. I'm trying to find rows that have an h in at least 2 of the greetings.

greeting1 greeting2 greeting3 farewell1 farewell2
hi hello hey there goodbye peace
hi sup yo Au revoir see ya
yo hola ayyyy hang tight reverse-hi
hola sup hello see ya peace
hello yo hola hang tight ciao
ayyy yo hola hang tight ciao

And below are the rows I'd like to grab:

greeting1 greeting2 greeting3 farewell1 farewell2
hi hello hey there goodbye peace
hola sup hello see ya peace
hello yo hola hang tight ciao

(I italicized and bolded for easier ability to see)

I'm just learning SQL, so this might be basic. I was having some difficulties. I know getting the rows and checking all greeting1 would look something like:

SELECT *
FROM
  GreetingsAndFarewell
WHERE
  greetings1 LIKE '%h%'
  AND greetings2 LIKE '%h%'
  AND greetings3 LIKE '%h%'

But what if I only care if at least 2 greetings have an h in them?

Here are some of the similar questions I've seen, but I don't think either of them really address this:

5760335 31577623

Dale K
  • 25,246
  • 15
  • 42
  • 71
radiomime
  • 118
  • 1
  • 1
  • 11

2 Answers2

2

You could use try summing CASE expressions which check for the letter h in each of the 3 greetings:

SELECT *
FROM GreetingsAndFarewell
WHERE CASE WHEN greetings1 LIKE '%h%' THEN 1 ELSE 0 END +
      CASE WHEN greetings2 LIKE '%h%' THEN 1 ELSE 0 END +
      CASE WHEN greetings3 LIKE '%h%' THEN 1 ELSE 0 END >= 2;

On BigQuery you might be able to directly sum boolean expressions and simplify to:

SELECT *
FROM GreetingsAndFarewell
WHERE greetings1 LIKE '%h%' + greetings2 LIKE '%h%' + greetings3 LIKE '%h%' >= 2;
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

consider below approach (BigQuery)

select *
from GreetingsAndFarewell
where (
  select count(1)
  from unnest([greeting1, greeting2, greeting3]) greeting
  where greeting like '%h%'
) >= 2    

if applied to sample data in your question - output is

enter image description here

Mikhail Berlyant
  • 165,386
  • 8
  • 154
  • 230