简体   繁体   English

sql 删除包含“test”的行但保留包含“contest”的行

[英]sql remove rows contain 'test' but keep rows contain 'contest'

I want to remove all campaign containing the word 'test' in their campaign names but keep the campaigns that have 'contest' in their names.我想删除所有活动名称中包含“测试”一词的活动,但保留名称中包含“竞赛”的活动。 Some campaigns don't have either test or contest in their names and i'd like to keep them as well.有些活动的名称中既没有测试也没有竞赛,我也想保留它们。 How can I achieve this in sql in Snowflake environment?如何在 Snowflake 环境中的 sql 中实现此目的? Since upper/lower case can be completely random I'm thinking of using NAME (ilike any('%_test_%', '%_test%', 'test_%', 'test')) however when I do this 'contest' is also removed which is not the result I want.由于大写/小写可以是完全随机的我正在考虑使用NAME (ilike any('%_test_%', '%_test%', 'test_%', 'test'))但是当我做这个'比赛'也被删除,这不是我想要的结果。

sample data:样本数据:

|                    Name                      |
|AuroraEnterprisesInc_AuroraSepTestCampaign    |
|_TEST                                         |
|test                                          |
|001_test_XP_GutterDoctor_June-OctoberCampaign|
|FR_TEST_                                      |
|002_XP_HunterKnifton_21ElectionContestCampaign|
|001_XP_Kijiji_BuyandSell                      |

the result should be结果应该是

|                    Name                      |
|002_XP_HunterKnifton_21ElectionContestCampaign|
|001_XP_Kijiji_BuyandSell                      |

You can use a regular expression in your where clause to filter these rows:您可以在 where 子句中使用正则表达式来过滤这些行:

select * from T1 where
regexp_instr(NAME, '\\btest\\b|_test_|\\btest_|_test\\b', 1, 1, 0, 'i') = 0;
NAME姓名
002_XP_HunterKnifton_21ElectionContestCampaign 002_XP_HunterKnifton_21ElectionContestCampaign
001_XP_Kijiji_BuyandSell 001_XP_Kijiji_买卖

If I understand correctly you want to exclude all records where the NAME column contains the substring "test" unless that substring is specifically part of the larger string "contest".如果我理解正确,您想排除 NAME 列包含 substring“test”的所有记录,除非 substring 是较大字符串“contest”的特定部分。

In other words, you're not trying to only exclude the stand-alone word "test", but any record that contains "test" not preceded by "con".换句话说,您不仅要排除单独的单词“test”,还要排除任何包含“test”但前面没有“con”的记录。 Eg, you want to exclude SeptTestCampaign and testament but retain SeptContestCampaign or contestament .例如,您想排除SeptTestCampaigntestament但保留SeptContestCampaigncontestament

Assuming that's correct, a WHERE clause such as:假设这是正确的,WHERE 子句如:

 SELECT ...
 WHERE ((name ILIKE '%contest%') OR (NOT (name ILIKE '%test%')))

should suffice.应该足够了。 (I've added parens just to make the logical grouping unambiguous but most or maybe all of those are technically optional.) (我添加了括号只是为了使逻辑分组明确无误,但大多数或可能所有这些在技术上都是可选的。)

The first criterion ( name ILIKE '%contest%' ) yields the set of records that expressly include the substring "contest".第一个条件( name ILIKE '%contest%' )产生明确包含 substring“竞赛”的记录集。

The second ( NOT name ILIKE '%test%' ) yields the set of all records that do not contain the substring "test" at all.第二个 ( NOT name ILIKE '%test%' ) 产生完全不包含 substring “测试”的所有记录集。

The OR operator yields the union of those two sets. OR运算符生成这两个集合的并集。

Or if you want to delete the test records rather than just filtering them out of a result set, reverse the conditions:或者,如果您想删除测试记录而不是仅仅将它们从结果集中过滤掉,请反转条件:

 DELETE FROM my_table 
 WHERE (name ILIKE '%test%') AND (NOT (name ILIKE '%contest%'))

One small caveat: if a record contains both contest and (independently) test , the logic above will include that record in the final results.一个小警告:如果记录同时包含contest和(独立) test ,则上面的逻辑将在最终结果中包含该记录。 Eg, a record with the NAME Test_ElectionContestCampaign will be included in the select (and will not be removed by the delete).例如,名称Test_ElectionContestCampaign的记录包含在 select 中(并且不会被删除删除)。 Based on your context I assume that's unlikely enough to be OK, but if you literally want to exclude all records that contain any instance of the substring test without a leading con please clarify.根据你的上下文,我认为这不太可能是好的,但如果你真的想排除包含 substring test的任何实例的所有记录而没有领先的con ,请澄清。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM