Sql 我正在向API发送一个参数列表(比如名称),参数列表将检查数据库并返回值
我正在向API发送一个参数列表(比如名称),参数列表将检查数据库并返回值 例:2 Med_疾病表 中到疾病表 我的查询是使用like命令仅拾取第一行,而不是其他行,我希望所有第一、第二和第三行都由A、B和C组成。我使用的是R和SQLoracle。 P.N:之前不知道该列表中可以包含多少令牌。Sql 我正在向API发送一个参数列表(比如名称),参数列表将检查数据库并返回值,sql,r,oracle,Sql,R,Oracle,我正在向API发送一个参数列表(比如名称),参数列表将检查数据库并返回值 例:2 Med_疾病表 中到疾病表 我的查询是使用like命令仅拾取第一行,而不是其他行,我希望所有第一、第二和第三行都由A、B和C组成。我使用的是R和SQLoracle。 P.N:之前不知道该列表中可以包含多少令牌。 请提供帮助。您好,这里有一个关于您试图实现的目标的查询 with your_data as ( select 'A,B,C,D' medicine_name, 'D1' disease from dua
请提供帮助。您好,这里有一个关于您试图实现的目标的查询
with your_data as (
select 'A,B,C,D' medicine_name, 'D1' disease from dual union
select 'A,C,B', 'D2' from dual union
select 'D,C,A,B', 'D3' from dual union
select 'D,A', 'D4' from dual
),
split as (select distinct regexp_substr(medicine_name,'[^,]+', 1, level) as medicine_name, disease from your_data
connect by regexp_substr(medicine_name,'[^,]+', 1, level) is not null),
aggr as (
select disease, listagg(medicine_name,',') within group (order by medicine_name) medicine_name from split
group by disease),
input as (select distinct regexp_substr('A,B,C','[^,]+', 1, level) as medicine_name from your_data
connect by regexp_substr('A,B,C','[^,]+', 1, level) is not null),
aggr_input as (
select listagg(medicine_name,',') within group (order by medicine_name) medicine_name from input)
select a.* from aggr a
join aggr_input ai on ai.medicine_name = substr(a.medicine_name,1,length(ai.medicine_name))
只需将您的数据替换为“从医学疾病中选择医学名称、疾病”
以及带有输入参数的硬编码“A、B、C”
让我知道它是如何运行的:-@ted感谢您的回复,但正如我之前提到的,我的数据集是动态的,它可以由3个令牌、4个令牌或任意数量的令牌组成,因此在这种情况下该怎么办…@KoushikRoy是的,代码支持任意数量的逗号分隔符tokens@KoushikRoy很高兴能帮上忙。请别忘了把答案标为正确。
with your_data as (
select 'A,B,C,D' medicine_name, 'D1' disease from dual union
select 'A,C,B', 'D2' from dual union
select 'D,C,A,B', 'D3' from dual union
select 'D,A', 'D4' from dual
),
split as (select distinct regexp_substr(medicine_name,'[^,]+', 1, level) as medicine_name, disease from your_data
connect by regexp_substr(medicine_name,'[^,]+', 1, level) is not null),
aggr as (
select disease, listagg(medicine_name,',') within group (order by medicine_name) medicine_name from split
group by disease),
input as (select distinct regexp_substr('A,B,C','[^,]+', 1, level) as medicine_name from your_data
connect by regexp_substr('A,B,C','[^,]+', 1, level) is not null),
aggr_input as (
select listagg(medicine_name,',') within group (order by medicine_name) medicine_name from input)
select a.* from aggr a
join aggr_input ai on ai.medicine_name = substr(a.medicine_name,1,length(ai.medicine_name))