请参考SQL Oracle:如何在列中查找与特定 id 匹配的记录
现在我的查询是在类似的行:我的表有以下数据:
现在我想找到将 asso_entity_id 作为 3 个值的组合的记录。例如:- 如果我从我的 jsp 中选择 asso_entities 为 30000、30001 和 80002(以任何顺序),我应该得到上表的第一条记录。
请参考SQL Oracle:如何在列中查找与特定 id 匹配的记录
现在我的查询是在类似的行:我的表有以下数据:
现在我想找到将 asso_entity_id 作为 3 个值的组合的记录。例如:- 如果我从我的 jsp 中选择 asso_entities 为 30000、30001 和 80002(以任何顺序),我应该得到上表的第一条记录。
请检查查询...有点长...将尝试缩短它。
with test as (
select * from YOURTABLE
)
SELECT distinct DATASETNAME FROM(
select x.*, COUNT(*) OVER (partition by DATASETNAME ORDER BY DATASETNAME) CNT From(
select DATASETNAME, regexp_substr (ASSO_ENTITY_ID, '[^|]+', 1, row_number() OVER (partition by DATASETNAME ORDER BY DATASETNAME)) split
from test
connect by level <= length (regexp_replace (ASSO_ENTITY_ID, '[^|]+')) + 1
)x where SPLIT IS NOT NULL
)xx
WHERE SPLIT IN ('300000', '300001', '800002') AND
CNT =3;
techdo 示例的简化版本:
这是测试表结构:
ID DATASET_NAME DATASET_VAL
------------------------------------------
1 DATASET1 3000 | 30001 | 80002
2 DATASET1 3000 | 80002
3 DATASET1 3000 | 80002
SELECT LISTAGG(str, ' | ') WITHIN GROUP (ORDER BY str) asso_ety_id
FROM
(
SELECT DISTINCT id, dataset_name
, TRIM(REGEXP_SUBSTR (dataset_val, '[^|]+', 1, LEVEL)) str
FROM your_tab
CONNECT BY LEVEL <= LENGTH(REGEXP_REPLACE(dataset_val, '[^|]+')) + 1
)
WHERE str IN ('80002', '30001', '3000') -- in any order --
-- AND id = 1 -- optional --
/
输出:
3000 | 30001 | 80002
继我对您上一个问题的回答之后,继续使用它作为示例,因为我可以看到正在执行的底层查询listagg
......您可以匹配多个值,计算您匹配的数量,并应用进一步的过滤器以确保您匹配所有这些。就像是:
select distinct role_id, role_name, active, companyName, permission_id,
permission_name, rn, total_rows, roleCreated
from (
select t.*,
count(raw_permission_id) over (partition by role_id) as cnt
from (
select r.role_id,
r.role_name,
r.active,
decode(r.entity_type_id, 1000, m.name, 3000, cour.name,
4000, 'Ensenda') companyName,
p.permission_id as raw_permission_id,
listagg(p.permission_id, ' | ')
within group (order by p.permission_id)
over (partition by r.role_id) permission_id,
listagg(p.permission_name, ' | ')
within group (order by p.permission_id)
over (partition by r.role_id) permission_name,
dense_rank() over (order by r.created_ts desc) as rn,
count(distinct r.role_id) over () as total_rows,
r.created_ts roleCreated
from t_role r
left join t_role_permission rp ON r.role_id = rp.role_id
left join t_permission p ON rp.permission_id = p.permission_id
left join merchant m on r.entity_id = m.merchantkey
left join courier cour on r.entity_id = cour.courierkey
) t
where raw_permission_id in (301446, 301445)
)
where cnt = 2
and rn between 1 and 100
order by roleCreated desc;
与先前答案的唯一区别是查询的外两层:
select ...
from (
select t.*,
count(raw_permission_id) over (partition by role_id) as cnt
from (
... -- no changes here
) t
where raw_permission_id in (301446, 301445)
)
where cnt = 2
...
所以它现在正在寻找两种可能的权限,并计算 - 分析,因此对于每个角色 - 匹配了多少。外部查询检查匹配的数字是否为 2,显然您必须根据您尝试匹配的选项数量进行调整。
对于这个问题,它会添加如下内容:
count(raw_asso_entity_id) over (partition by <some_id>) as cnt
...
where raw_asso_id in (30000, 30001 and 80002)
...
where cnt = 3