tags:

views:

41

answers:

1

Hi, I'm having trouble with a query I'm trying to do. I'm trying to select values that come up in to result sets.

I'm currently dealing with two queries:

A)

SELECT     /*+ RULE */ pi.compressed_name, pi.phn, to_char(pi.date_of_birth , 'YYYY/MM/DD') as date_of_birth, gen.description as gender
        , to_char(pe.started_on , 'YYYY/MM/DD' ) as medicare_eligibility_start , to_char(pe.ended_on , 'YYYY/MM/DD' ) as medicare_eligibility_end
FROM    medcrtr.forest_node fnpppp,
    medcrtr.forest_node fnppp,
    medcrtr.forest_node fnpp,
    medcrtr.forest_node fnp,
    medcrtr.forest_node fn,
    medcrtr.group_member gm,
    medcrtr.group_type gt,
    medcrtr.program_eligibility pe,
    person_index pi,
    gender_type gen
WHERE   gm.entity_type_id = 1 --:P_PERSON_ENTITY_TYPE_ID
    AND gen.id = pi.gender_code
    AND gt.id = gm.group_id
    AND gt.category_id = 1 --icgroupmemebrcategory :P_GROUP_CATEGORY_ID
    AND fn.source_id = group_id
    AND fn.entity_type_id = 3 --icGOM:P_ENTITY_TYPE_ID
    AND fnp.id = fn.parent_id
    AND fnpp.id = fnp.parent_id
    AND fnppp.id = fnpp.parent_id
    AND fnpppp.id = fnppp.parent_id
    AND pe.person_id = gm.source_id
    AND pe.sub_program_id = fnpp.parent_id
    AND pi.person_id = gm.source_id
    AND fnppp.id = 1169
    AND (gm.ended_on >= SYSDATE OR gm.ended_on IS NULL)

B) Same as above other than second last line, in which

AND fnppp.id = 1715

So now the first query returns 1536 records, and the second returns 2067. There are however records(people) that come up in both queries. What I want to do is single them out. (Essentially I want to do an intersection so I"ll look into that)

So far I've tried to do the following:

-I've added the following to query A:

AND pi.person_id NOT IN (SELECT    /*+ RULE */ pi.person_id FROM ... )
where the query inside the brackets is query B (other than for the first line). This returns about 10 rows (I'm not sure that's accurate).

-I then ran the same query except with NOT IN. That gives me a result of about 200(not sure if this is accurate).

Now those two numbers obviously don't add up to 1536, which I think should happen? So obviously one or both of them is wrong.

Can anyone tell what I'm doing wrong? Thanks for your help.

+1  A: 

If you are truly looking for records that match between two result sets, check out information about the SQL INTERSECT command.

Looking at your query, though, I'm not sure that's what you want. If one record has fnppp.id = 1169, then how can the same record have fnppp.id = 1715?

Are you possibly looking for something that's more like SQL UNION?

Dave McClelland
Thanks, I don't know how I forgot about INTERSECT, that's what I needed.
The_Denominater