Search code examples
sqloracleconditional-aggregation

Oracle query with group


I have a scenario where I need to fetch all the records within an ID for the same source. 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. Also, we need to fetch only those records where at least one record in that ID group with 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'.

Also within the group which has satisfied the above condition, I need one more condition for 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'

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

Solution

  • 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. I do this in a CTE below, and then join back to your original table to return full matching records.

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