I have a query that returns records that have duplicate names within a given DBSTATUS
. I used to do this with a direct connection to the remote oracle database server with this query. (Names changed to protect the innocent, but otherwise, this is my code.)
SELECT SUBSTR(NAME, 1, 3) AS CATID,
TABLE1.NAME,
TABLE1.DESC1 AS DESCRIPTION,
TABLE1.DBKEY,
TABLE1.DBSTATUS,
ISSUES.NAME_CT
FROM (SELECT NAME, DBSTATUS, COUNT(NAME) AS NAME_CT
FROM TABLE1
WHERE DBSTATUS IN (1,2)
GROUP BY NAME, DBSTATUS
HAVING COUNT(NAME) > 1) ISSUES,
TABLE1
WHERE ISSUES.NAME = TABLE1.NAME
AND ISSUES.DBSTATUS = TABLE1.DBSTATUS
ORDER BY CATID, NAME, DBKEY
The requirements have changed and now I need to exclude a list of CATID
s from this report, but only when they have a DBSTATUS
of 2
. This is what I came up with, but I don't much like the duplicated select in the WHERE
statement. Since the exclusion table resides on the local server, I have switched to an OPENQUERY
instead of a direct connection to the remote server. How can I improve this?
SELECT *
FROM OPENQUERY(REMOTE_ORACLE_SERVER,
'SELECT SUBSTR(NAME, 1, 3) AS CATID,
TABLE1.NAME,
TABLE1.DESC1 AS DESCRIPTION,
TABLE1.DBKEY,
TABLE1.DBSTATUS,
ISSUES.NAME_CT
FROM (SELECT NAME, DBSTATUS, COUNT(NAME) AS NAME_CT
FROM TABLE1
WHERE DBSTATUS IN (1,2)
GROUP BY NAME, DBSTATUS
HAVING COUNT(NAME) > 1) ISSUES,
TABLE1
WHERE ISSUES.NAME = TABLE1.NAME
AND ISSUES.DBSTATUS = TABLE1.DBSTATUS
ORDER BY CATID, NAME, DBKEY
')drv
WHERE (CATID IN(SELECT CATID FROM RACK_CATIDS) AND DBSTATUS = 1)
OR (CATID NOT IN(SELECT CATID FROM RACK_CATIDS))