SQL,Oracle11g:查找与特定ID集匹配的记录

时间:2013-03-06 11:41:27

标签: sql oracle oracle11gr2

请参阅SQL Oracle : how to find records maching a particular id in the column

现在我的查询是类似的: 我的表格包含以下数据:enter image description here

现在我想找到将asso_entity_id组合为3个值的记录。 例如: - 如果我从我的jsp选择asso_entities为30000,30001和80002(按任意顺序),我应该得到上表的第一条记录。

3 个答案:

答案 0 :(得分:1)

请检查查询...位长......会尝试缩短查询。

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;

答案 1 :(得分:0)

继续我对你上一个问题的回答,并继续使用它作为一个例子,因为我可以看到正在执行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

答案 2 :(得分:0)

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