Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

sql - How to remove certain results from my search count?

I received a few irrelevant results to my count that I would like to remove from my output.

When using only the first "where lower (person) not like '%c%'" worked but when adding person b, c etc. as shown it gave me the original irrelevant results.

What should I change?

SELECT
    person,
    COUNT(1) AS count_mentions,
    COUNT(DISTINCT url) AS count_distinct_urls
FROM
    project.dataset.keyword
WHERE
    LOWER(person) NOT LIKE '%b%' 
    OR LOWER(person) NOT LIKE '%c%' 
    OR LOWER(person) NOT LIKE '%e%' 
    OR LOWER(person) NOT LIKE '%f%' 
GROUP BY
    person
ORDER BY
    count_mentions DESC
LIMIT
    5;

Expected results:

Row person  count_mentions  count_distinct_urls 
-----------------------------------------------
1     a          ---               ----
2     d 
3     g
4     h
5     i
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You want AND, not OR:

where lower(person) not like '%b%' and
      lower(person) not like '%c%' and
      lower(person) not like '%e%' and
      lower(person) not like '%f%'  

This is logically equivalent to:

where not (lower(person) like '%b%' or
           lower(person) not like '%c%' or
           lower(person) not like '%e%' or
           lower(person) not like '%f%' 
          ) 

Or more concisely:

 where not regexp_contains(person, '[BbCcEeFf]')

Or if you want to be a bit inscrutable:

where not regexp_contains(person, '(?i)[bcef]')

The (?i) makes the pattern matching case-insensitive.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...