Is that what are you looking for?
I just added a WHERE
clause to your inner query:
SELECT idnumber
WHERE recstat <> 'D'
FROM table GROUP by idnumber
HAVING count(idnumber) >= 2
Question
+----------+---------+------------+-----------+
| MEMRECNO | RECSTAT | IDSRCRECNO | IDNUMBER |
+----------+---------+------------+-----------+
| 556787 | D | 5 | 956645789 |
| 123456 | A | 5 | 956645789 |
| 546578 | A | 5 | 462454322 |
| 262441 | A | 4 | 462454322 |
| 657855 | D | 3 | 462454322 |
| 746877 | A | 5 | 654988844 |
| 989455 | A | 2 | 654988844 |
| 444863 | A | 1 | 654988844 |
+----------+---------+------------+-----------+
I am attempting to write a query to select records where two or more sources (IDSRCRECNO) have same IDNUMBER. I have started the query with something along the lines of:
WHERE idnumber IN (SELECT idnumber from table GROUP by idnumber HAVING count(idnumber) >= 2
Expected Results:
MEMRECNO 546578 and 262441 included in results. This is because both records are active, the IDNUMBER's are identical and the IDSRCRECNO is different.
MEMRECNO 556787 and 123456 would not be included because even though the IDNUMBERS match, one of the records/recstat is Deleted (D).
Memrecno 262441 and 657855 would not be included because even though the IDNUMBERS match and IDSRCRECNO is different, one of the records/recstat is Deleted (D).
Memrecno 262441 and 657855 would not be included because even though the IDNUMBERS match and IDSRCRECNO is different, one of the records/recstat is Deleted (D).
Memrecno 746877, 989455, 444863 would be included because all three records are active, IDSRCRECNO is different and IDNUMBERs match.
Thanks in advance!
Solution
Is that what are you looking for?
I just added a WHERE
clause to your inner query:
SELECT idnumber
WHERE recstat <> 'D'
FROM table GROUP by idnumber
HAVING count(idnumber) >= 2
OTHER TIPS
I would do this like:
with data as (
select 556787 memrecno, 'D' recstat, 5 idsrcrecno, 956645789 idnumber from dual union all
select 123456 memrecno, 'A' recstat, 5 idsrcrecno, 956645789 idnumber from dual union all
select 546578 memrecno, 'A' recstat, 5 idsrcrecno, 462454322 idnumber from dual union all
select 262441 memrecno, 'A' recstat, 4 idsrcrecno, 462454322 idnumber from dual union all
select 657855 memrecno, 'D' recstat, 3 idsrcrecno, 462454322 idnumber from dual union all
select 746877 memrecno, 'A' recstat, 5 idsrcrecno, 654988844 idnumber from dual union all
select 989455 memrecno, 'A' recstat, 2 idsrcrecno, 654988844 idnumber from dual union all
select 444863 memrecno, 'A' recstat, 1 idsrcrecno, 654988844 idnumber from dual
)
select *
from (
select memrecno, recstat, idsrcrecno, idnumber, count(distinct idsrcrecno) over (partition by idnumber) rec_cnt
from data
where recstat = 'A'
)
where rec_cnt > 1;
Which returns:
MEMRECNO RECSTAT IDSRCRECNO IDNUMBER REC_CNT
---------- ------- ---------- ---------- ----------
262441 A 4 462454322 2
546578 A 5 462454322 2
444863 A 1 654988844 3
989455 A 2 654988844 3
746877 A 5 654988844 3
This assumes you only want to see Active records. The query would have to be changed if for example MEMRECNO = 444863 was not Active, but you still wanted it to show up (since there would still be 2 records with the same IDNUMBER and different IDSRCRECNO). Just comment if that is what you are looking for and I will update this accordingly.
You can also achieve this without a sort, using a query that would likely use a hash semijoin for large data quantities:
select *
from my_table t1
where recstat = 'A' and
exists (
select null
from my_table t2
where t2.IDNUMBER = t1.IDNUMBER and
t2.MEMRECNO != t1.MEMRECNO and
t2.RECSTAT = 'A')
I think your WHERE
clause should be
WHERE idnumber in ( SELECT idnumber
FROM ( SELECT count(*), idnumber
FROM table
GROUP by idnumber
HAVING count(*) >= 2
) temp
)