2013-03-06 38 views
2

的請參考SQL Oracle : how to find records maching a particular id in the columnSQL,Oracle11g的:找到匹配的特定組ID的

現在我的查詢是在類似的路線記錄: 我的表中有如下數據:enter image description here

現在我想找到記錄將asso_entity_id作爲3個值的組合。 例如: - 如果我從jsp選擇asso_entities爲30000,30001和80002(以任何順序),我應該得到上表的第一條記錄。

+1

I s真的是單個表中的數據,還是像上一個問題那樣的'listagg'查詢的結果? – 2013-03-06 12:20:25

+0

@AlexPoole:你是對的。其結果listagg查詢 – Bhuvan 2013-03-06 12:32:43

+0

你能給我表格結構嗎? – 2013-03-06 13:05:01

回答

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; 
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 
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