我的数据库中有这些数据:
RELATION VALUE
-------- -----
COD_1 VALUE1
COD_1 VALUE2
COD_1 VALUE3
COD_2 VALUE4
COD_2 VALUE5
COD_3 VALUE6
COD_3 VALUE7
我需要获取适合给定值集的关系。例如,如果我具有以下输入值:
[0]
VALUE1
VALUE2
VALUE3
已恢复的关系必须为COD_1,因为所有输入值的关系都以COD_1为准。
如果我有:
[0]
VALUE1
VALUE2
VALUE3
[1]
VALUE6
VALUE7
恢复的值必须为COD_1和COD_3。
是否可以通过SQL选择查询来做到这一点?
非常感谢您
编辑:输入修改
答案 0 :(得分:1)
我认为这可以解决您的问题
select RELATION from
(SELECT RELATION, LISTAGG(value, ',') WITHIN GROUP (ORDER BY RELATION) AS ip from
input_table
group by RELATION)A
where ip = 'VALUE1,VALUE2,VALUE3'
答案 1 :(得分:1)
假设您需要输入中包含每个值的所有关系,这是一个查询
WITH
matching_values
AS
(SELECT *
FROM mydata
WHERE VALUE IN ('VALUE1', 'VALUE2', 'VALUE3', 'VALUE6', 'VALUE7'))
SELECT DISTINCT a.relation
FROM (SELECT relation,
COUNT (DISTINCT VALUE) AS n_vals
FROM mydata
GROUP BY relation) a,
(SELECT relation,
COUNT (DISTINCT VALUE) AS n_vals
FROM matching_values
GROUP BY relation) b
WHERE a.relation = b.relation
AND a.n_vals = b.n_vals;
输入值为1,2,3,6,7时,我们得到
+----------+
| RELATION |
+----------+
| COD_3 |
| COD_1 |
+----------+
使用值为1,2,6,7的输入(注意:缺少值3,因此COD_1不完整),我们得到
+----------+
| RELATION |
+----------+
| COD_3 |
+----------+
这是小提琴 SQLFiddle
答案 2 :(得分:0)
怎么样:
select distinct RELATION from TABLE where values in ('VALUE_1', 'VALUE_2', 'VALUE_3')
答案 3 :(得分:0)
我希望您至少具有11gr2版本才能使用递归子查询。您可以使用它来获取关系的所有不同组合。这是一个简短的示例,说明如何生成这些组合:
with test_data as (
select 'COD_1' relation from dual union all
select 'COD_2' from dual union all
select 'COD_3' from dual
),
recursive_subq(relation, codes) as (
select relation,
relation codes
from test_data
union all
select t.relation,
codes||','||t.relation
from recursive_subq rs,
test_data t
where rs.relation < t.relation
)
select codes
from recursive_subq c
它返回
COD_1
COD_2
COD_3
COD_1,COD_2
COD_1,COD_3
COD_2,COD_3
COD_1,COD_2,COD_3
主查询将不同关系组合的值连接起来,并将其与所需的(输入数据)进行比较:
with initial_data as (
select 'COD_1' relation, 'VALUE1' val from dual union all
select 'COD_1', 'VALUE2' from dual union all
select 'COD_1', 'VALUE3' from dual union all
select 'COD_2', 'VALUE4' from dual union all
select 'COD_2', 'VALUE5' from dual union all
select 'COD_3', 'VALUE6' from dual union all
select 'COD_3', 'VALUE7' from dual
),
input_data as (
select 'VALUE1' val from dual union all
select 'VALUE2' val from dual union all
select 'VALUE3' val from dual union all
select 'VALUE7' val from dual union all
select 'VALUE6' val from dual
),
input_data_result as (
select listagg(val,',') within group (order by val) input_values_listagg
from input_data
),
distinct_relations as (
select distinct relation
from initial_data
),
initial_data_as_collection as (
select relation,
cast(
multiset (
select val from initial_data where relation = d.relation
)
as sys.odcivarchar2list
) ss
from distinct_relations d
),
-- this is the recursive subquery
recursive_subq(relation, codes, values_listagg) as (
select relation,
relation codes,
listagg(val,',') within group(order by val) values_listagg
from initial_data d
group by relation
union all
select dr.relation,
codes||','||dr.relation codes,
(
select listagg(ind.val,',') within group (order by ind.val)
from initial_data ind
where ','||codes||','||dr.relation||',' like '%,'||ind.relation||',%'
) values_listagg
from recursive_subq rs,distinct_relations dr
where rs.relation < dr.relation
)
select codes
from recursive_subq c,
input_data_result r
where c.values_listagg = r.input_values_listagg