Oracle将多行的两个参数提供给子查询(或从查询到另一个)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle将多行的两个参数提供给子查询(或从查询到另一个)相关的知识,希望对你有一定的参考价值。
我正在尝试从第一个查询到第二个查询提供两个参数:
SELECT t1.FAM,t2.CODE FROM (SELECT t1.FAM, t2.CODE FROM (SELECT num_family FROM family) t1, (SELECT num_code FROM code) t2)
所以我有两个参数(多行):
FAM1 | CODE1
FAM1 | CODE2
FAM1 | CODE3
FAM2 | CODE1
FAM2 | CODE2
FAM2 | CODE3
所以这是我的两个参数,我想把这两个参数放到第三个请求:
SELECT count(*) FROM stats WHERE num_fam LIKE '%FAM1%' AND num_code LIKE '%FAM1%';
具有 :
FAM1 | CODE1 | 5
FAM1 | CODE2 | 2
FAM1 | CODE3 | 0
FAM2 | CODE1 | 9
FAM2 | CODE2 | 4
FAM2 | CODE3 | 1
但我不明白如何组合这两个查询并将第一个查询中的两个参数提供给第二个:我尝试使用子查询和内部连接,但这并不成功......!
答案
以下是我的想法...请检查这是否有效:
SELECT COUNT (*)
FROM stats
WHERE (num_fam, num_code) IN
(SELECT t1.FAM, t2.CODE
FROM (SELECT num_family FROM family) t1,
(SELECT num_code FROM code) t2)
AND NUM_FAM LIKE '%FAM1%' --I assumed this is a required filter .
AND NUM_CODE LIKE '%FAM1%';
另一答案
您可以使用交叉连接和内连接
SELECT t1.num_family, t2.CODE, count(*)
FROM family t1
CROSS JOIN code t2
INNER JOIN stats t3 ON t1.num_family LIKE '%FAM1%' AND t2.num_code LIKE '%FAM1%'
GROUP BY t1.num_family, t2.CODE
另一答案
听起来你正在交叉加入前两个表,例如使用示例数据来匹配您显示为输出的内容:
-- CTE for implied sample data
with family (num_family) as (
select 'FAM1' from dual
union all select 'FAM2' from dual
),
code (num_code) as (
select 'CODE1' from dual
union all select 'CODE2' from dual
union all select 'CODE3' from dual
)
-- actual query (edited from what you showed to be valid)
--SELECT t1.FAM,t2.CODE FROM (SELECT t1.FAM, t2.CODE FROM (SELECT num_family FROM family) t1, (SELECT num_code FROM code) t2)
SELECT t1.FAM, t2.CODE FROM (SELECT num_family as fam FROM family) t1, (SELECT num_code as code FROM code) t2
/
FAM CODE
---- -----
FAM1 CODE1
FAM1 CODE2
FAM1 CODE3
FAM2 CODE1
FAM2 CODE2
FAM2 CODE3
甚至不需要那些子查询,因为它与以下内容相同:
select f.num_family, c.num_code from family f cross join code c
然后你想得到liste
表中匹配的stats
值的计数,其中系列和代码值都嵌入在该字符串中,前面有一个哈希:
我必须按照'%#'||的情况来做案例t1.FAM ||'%'那么(例如,当列出'%#'|| t2.CODE ||'%'那么1 ELSE 0 END)ELSE 0 END而不是SELECT COUNT(*)FROM stats
您可以在连接条件中使用外连接和布尔逻辑执行操作,而不是尝试使用case表达式:
-- CTE for implied sample data
with family (num_family) as (
select 'FAM1' from dual
union all select 'FAM2' from dual
),
code (num_code) as (
select 'CODE1' from dual
union all select 'CODE2' from dual
union all select 'CODE3' from dual
),
stats (liste) as (
select 'abc #FAM1 def #CODE1 ghi' from dual connect by level <= 5
union all select 'jkl #CODE2#FAM1 mno' from dual connect by level <= 2
union all select '#FAM2#CODE1' from dual connect by level <= 9
union all select '#FAM2#CODE2' from dual connect by level <= 4
union all select '#FAM2#CODE3' from dual
union all select '#FAM2#CODE4' from dual -- no match
union all select '#FAM3#CODE1' from dual -- no match
)
-- actual query
select f.num_family, c.num_code, count(s.liste) as matches
from family f
cross join code c
left join stats s on s.liste like '%#'|| f.num_family ||'%'
and s.liste like '%#'|| c.num_code ||'%'
group by f.num_family, c.num_code
order by f.num_family, c.num_code;
NUM_ NUM_C MATCHES
---- ----- ----------
FAM1 CODE1 5
FAM1 CODE2 2
FAM1 CODE3 0
FAM2 CODE1 9
FAM2 CODE2 4
FAM2 CODE3 1
如果族或代码值具有重叠的根或意外的部分匹配,则like
模式将成为问题。例如。如果你有liste
的#FAM10#CODE20
值,它仍然会匹配FAM1
和CODE2
。你可以切换到regexp_like()
,如果你可以定义一个模式,以避免那些错误的匹配(例如后跟空格/标点符号/ EOL)。
以上是关于Oracle将多行的两个参数提供给子查询(或从查询到另一个)的主要内容,如果未能解决你的问题,请参考以下文章