简体   繁体   English

Oracle查询组

[英]Oracle query with group

I have a scenario where I need to fetch all the records within an ID for the same source. 我有一种情况,我需要为同一源获取ID内的所有记录。 Given below is my input set of records 以下是我输入的记录集

ID  SOURCE  CURR_FLAG       TYPE
1   IBM       Y               P
1   IBM       Y               OF
1   IBM       Y               P
2   IBM       Y               P
2   TCS       Y               P
3   IBM     NULL              P
3   IBM     NULL              P
3   IBM     NULL              P
4   IBM     NULL              OF
4   IBM     NULL              OF
4   IBM      Y                ON

From the above settings, I need to select all the records with source as IBM within that same ID group.Within the ID group if there is at least one record with a source other than IBM, then I don't want any record from that ID group. 从上面的设置中,我需要在同一ID组中选择所有源为IBM的记录。在ID组中,如果至少有一条源不是IBM的记录,那么我不想从中获得任何记录ID组。 Also, we need to fetch only those records where at least one record in that ID group with curr_fl='Y' 另外,我们只需要获取那些ID组中至少一个记录(其中curr_fl ='Y')的那些记录

In the above scenario even though the ID=3 have a source as IBM, but there is no record with CURR_FL='Y', my query should not fetch the value.In the case of ID=4, it can fetch all the records with ID=4, as one of the records have value='Y'. 在上述情况下,即使ID = 3的来源是IBM,但没有CURR_FL ='Y'的记录,我的查询也不应获取该值。对于ID = 4的情况,它可以获取所有记录ID = 4,因为其中一条记录的值='Y'。

Also within the group which has satisfied the above condition, I need one more condition for source_type. 同样在满足以上条件的组中,对于source_type我还需要一个条件。 if there are records with source_type='P', then I need to fetch only that record.If there are no records with P, then I will search for source_type='OF' else source_type='ON' 如果有source_type ='P'的记录,那么我只需要获取该记录。如果没有P的记录,那么我将搜索source_type ='OF'否则source_type ='ON'

I have written a query as given below.But it's running for long and not fetching any results. 我写了如下查询,但是它运行了很长时间并且没有获取任何结果。 Is there any better way to modify this query 有没有更好的方法来修改此查询

select
    ID,
    SOURCE,
    CURR_FL,
   TYPE
from TABLE a
where 
    not exists(select 1 from TABLE B where a.ID = B.ID and source <> 'IBM')
    and exists(select 1 from TABLE C where a.ID = C.ID and CURR_FL = 'Y')  and
        (TYPE, ID) IN (
            select case  type when 1 then 'P' when 2 then 'OF' else 'ON' END TYPE,ID  from
            (select ID,
             max(priority) keep (dense_rank first order by priority asc) as type
      from ( select ID,TYPE,
               case TYPE 
                     when 'P' then 1
                     when 'OF' then 2
                     when 'ON' then 3
                end as priority 
           from TABLE where ID
            in(select ID from TABLE where CURR_FL='Y') AND SOURCE='IBM')
                group by ID)) 

I think you can just do a single aggregation over your table by ID and check for the yes flag as well as assert that no non IBM source appears. 我认为您可以按ID对表进行一次汇总,并检查yes标志,并断言没有出现非IBM源。 I do this in a CTE below, and then join back to your original table to return full matching records. 我在下面的CTE中执行此操作,然后再联接回您的原始表以返回完整的匹配记录。

WITH cte AS (
    SELECT
        ID,
        CASE WHEN SUM(CASE WHEN TYPE = 'P'  THEN 1 ELSE 0 END) > 0
             THEN 1
             WHEN SUM(CASE WHEN TYPE = 'OF' THEN 1 ELSE 0 END) > 0
             THEN 2
             WHEN SUM(CASE WHEN TYPE = 'ON' THEN 1 ELSE 0 END) > 0
             THEN 3 ELSE 4 END AS p_type
    FROM yourTable
    GROUP BY ID
    HAVING
        SUM(CASE WHEN CURR_FLAG = 'Y' THEN 1 ELSE 0 END) > 0 AND
        SUM(CASE WHEN SOURCE <> 'IBM' THEN 1 ELSE 0 END) = 0
)

SELECT t1.*
FROM yourTable t1
INNER JOIN cte t2
    ON t1.ID = t2.ID
WHERE
    t2.p_type = 1 AND t1.TYPE = 'P' OR
    t2.p_type = 2 AND t1.TYPE = 'OF' OR
    t2.p_type = 3 AND t1.TYPE = 'ON';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM