Hey guys,
I am trying to design a query that will do the following:
Return employees who have both N and K plan code values in their history table.
Sample table
SSN DATE EMPR_PLAN_CD
____ ____ _____________
11233333 2003-04-01 N
11233333 2005-05-01 K
In this example, the SSN would appear in my results because they have both N and K.
I came up with this query which does appear to work:
My problem is when I tack this code onto my larger more complex query, SQL bogs down, and never stops processing.
Is there another more efficient method to return rows with distinct values for the same SSN? (SSN is the primary key in the table) Thanks!
I am trying to design a query that will do the following:
Return employees who have both N and K plan code values in their history table.
Sample table
SSN DATE EMPR_PLAN_CD
____ ____ _____________
11233333 2003-04-01 N
11233333 2005-05-01 K
In this example, the SSN would appear in my results because they have both N and K.
I came up with this query which does appear to work:
Code:
select * from HISTORY A
where a.empr_plan_cd = 'N'
and exists (select 1 from HISTORY B
where B.SSN = A.SSN
and B.empr_plan_cd = 'K')
My problem is when I tack this code onto my larger more complex query, SQL bogs down, and never stops processing.
Is there another more efficient method to return rows with distinct values for the same SSN? (SSN is the primary key in the table) Thanks!