我有一个SQL函数,它使我返回一串逗号分隔的国家代码。
我已经在另一个表中配置了一些特定代码,并且以后可能会删除或添加更多。
我想检查逗号分开的字符串是否只是这些特定国家代码的组合。也就是说,如果该字符串具有除指定的代码以外的单个国家代码,则应返回true。
假设我在静态数据表GB和CH中配置了两个行。然后我需要以下结果:
的字符串 |
函数结果 |
gb |
false |
false |
false |
gb,ch |
false |
ch,gb |
false |
gb,fr |
true |
fr,es |
true |
es,ch |
true |
ch,gb,gb,es |
true |
我在Oracle 19c上,只能使用可用于此版本的功能。另外,我希望它得到优化。就像我可以检查字符串中的值数,然后计算每个特定代码。如果不匹配,则显然存在一些其他代码。但是我不想使用循环。
有人可以建议我一个更好的选择。
I have an SQL function that returns me a string of comma separated country codes.
I have configured some specific codes in another table and I may remove or add more later.
I want to check if the comma separated string is only the combination of those specific country codes or not. That said, if that string is having even a single country code other than the specified ones, it should return true.
Suppose I configured two rows in the static data table GB and CH. Then I need below results:
String from function |
result |
GB |
false |
CH |
false |
GB,CH |
false |
CH,GB |
false |
GB,FR |
true |
FR,ES |
true |
ES,CH |
true |
CH,GB,ES |
true |
I am on Oracle 19c and can use only the functions available for this version. Plus I want it to be optimised. Like I can check the number of values in string and then count for each specific code. If not matching then obviously some other codes are present. But I don't want to use loops.
Can someone please suggest me a better option.
发布评论
评论(5)
假设静态表中的所有国家代码以及逗号分隔字符串中的所有令牌始终都是两个字符串,您可以做这样的事情:
输出:
正则表达式替代逗号,空间,每两个 - 带有
null
的静态数据表中的字母乡村代码。如果整个过程的结果是null
,则CSV中的所有编码都在静态表中;这就是您需要测试的。假设保证了像GBCH这样的令牌(对于像“大障碍国家”这样的国家)不会被误认为是可以的,因为GB和CH分别可以。
Assuming that all country codes in the static table, as well as all tokens in the comma-separated strings, are always exactly two-letter strings, you could do something like this:
Output:
The regular expression replaces comma, space, and every two-letter country code from the static data table with
null
. If the result of the whole thing isnull
, then all coded in the csv are in the static table; that's what you need to test for.The assumptions guarantee that a token like GBCH (for a country like "Great Barrier Country Heat") would not be mistakenly considered OK because GB and CH are OK separately.
您可以将CSV列转换为表格并使用。例如
You can convert a csv column to a table and use EXISTS. For example
一种选择是逐一匹配国家代码,然后确定是否存在从提供的文字作为参数中的额外的非匹配国家 /地区。
以下一个具有完整加入的人将考虑上面的逻辑
One option is to match the country codes one by one, and then determine whether there exists an extra non-matched country from the provided literal as parameter.
The following one with FULL JOIN would help by considering the logic above
Demo
这是一个解决方案
db<>fiddle here
Here is one solution
db<>fiddle here
如果静态表中有少数值,那么最简单的方法可能不是将值与函数拆分,而是使用静态表中的所有值组合,使用:
对于示例数据:
输出:输出:
,然后您可以将简单的
案例
表达到字符串输出到组合:对于示例数据:输出:
输出:
db&lt;&gt; fiddle here
If you have a small number of values in the static table then the simplest method may not be to split the values from the function but to generate all combinations of values from the static table using:
Which, for the sample data:
Outputs:
Then you can use a simple
CASE
expression to your string output to the combinations:Which, for the sample data:
Outputs:
db<>fiddle here