I am trying to find an Oracle SQL to test whether a table (CONTACTS) contains two rows of data for each primary key (PK) called REF_NO.
The table has a PK called REF_NO and a second PK called CONT_IND. CONT_IND can be either 1 or 2 - no other values possible, including NULL.
For each REF_NO, the table should contain:
- one row where
CONT_IND = 1 - one row where
CONT_IND = 2
I'm trying to find an output that shows:
FAIL: REF_NO 'X' CONT_IND 1 IS MISSING
or
FAIL: REF_NO 'X' CONT_IND 2 IS MISSING
If both rows are present, the output should show:
PASS
CONT_INDas a key of any kind if it's only a binary indicator (1 or 0). If it were a key, then the table could have only two rows in it. Perhaps what OP meant was it's a second column in the primary key definition, so the PK might actually be (REF_NO,CONT_ID).(REF_NO,PRIMARY_CONTACT_ID, SECONDARY_CONTACT_ID)where both columns areNOT NULLand a check constraint ensures they aren't the same, and then have aCONTtable with surrogate keyCONTACT_IDthat defines them. That's a much more enforceable model than counting rows.