Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

sub-query

SELECT
            O.ORDER_MNEMONIC,
            EA.ALIAS,
            E.ACTIVE_STATUS_DT_TM,
         P.NAME_FULL_FORMATTED,
            P.POSITION_CD
FROM
            EKS_DLG_EVENT  E,
            PRSNL  P,
            ENCNTR_ALIAS  EA,
            ORDERS  O
WHERE
         bla...for join ...
         E.DLG_NAME = 'MUL_MED!DRUGFOOD'        
         AND E.DLG_PRSNL_ID = P.PERSON_ID
            AND E.ACTIVE_STATUS_DT_TM BETWEEN '7 days ago' AND 'today'
            AND P.POSITION_CD NOT IN (9620245, 9616239, 9616238)
         AND E.ENCNTR_ID = EA.ENCNTR_ID AND EA.ENCNTR_ALIAS_TYPE_CD = 141.00
            AND E.TRIGGER_ORDER_ID = O.ORDER_ID

O.ORDER_MNEMONIC and EA.ALIAS on SELECT can be identical whereas the others on SELECT can be different.
If the identical records with the same O.ORDER_MNEMONIC and EA.ALIAS ever exit before (i.e. 1 year ago, 1 month ago, etc),
any records after the first one should not be qualified (= should not be selected).

For instance,

ABC  1234567890  01/01/2005  John Doe DBA
ABC  1234567890  07/04/2005  Jane Doe DBA
   -> these two records have the same ABC  1234567890 (O.ORDER_MNEMONIC and EA.ALIAS), so
       only the 1st record should be qualified when the report is run.

There can be another record like:
ABC  1234567890  07/19/2006  Lisa Doe DBA

but, this record should not be selected even though this recod on 07/19/2006 meets all of the qualifications on WHERE clause
BECAUSE (ABC  1234567890  01/01/2005  John Doe DBA) exists.

It seems like subquery is to be used....
any ideas?
0
ethanjohnsons
Asked:
ethanjohnsons
  • 2
1 Solution
 
Brendt HessSenior DBACommented:
At the end of your WHERE clause, try adding:

..
    AND NOT EXISTS (
        SELECT *
        FROM EKS_DLG_EVENT  Ex,   -- NOTE:  This should really use the INNER JOIN / LEFT JOIN / RIGHT JOIN syntax, not the JOIN in the WHERE clause.
            PRSNL  Px,
            ENCNTR_ALIAS  EAx,
            ORDERS  Ox
        WHERE
            Ox.ORDER_MNEMONIC = O.ORDER_MNEMONIC
            AND EAx.ALIAS = EA.ALIAS
            AND (
                Ex.ACTIVE_STATUS_DT_TM < E.ACTIVE_STATUS_DT_TM
                OR (
                    Ex.ACTIVE_STATUS_DT_TM = E.ACTIVE_STATUS_DT_TM
                    AND Px.NAME_FULL_FORMATTED < P.NAME_FULL_FORMATTED
                )
            )
            AND   --->>       bla...for join ...
            AND E.DLG_NAME = 'MUL_MED!DRUGFOOD'        
            AND E.DLG_PRSNL_ID = P.PERSON_ID
            AND E.ACTIVE_STATUS_DT_TM BETWEEN '7 days ago' AND 'today'
            AND P.POSITION_CD NOT IN (9620245, 9616239, 9616238)
            AND E.ENCNTR_ID = EA.ENCNTR_ID
            AND EA.ENCNTR_ALIAS_TYPE_CD = 141.00
            AND E.TRIGGER_ORDER_ID = O.ORDER_ID
    )
 
0
 
ethanjohnsonsAuthor Commented:
why do you put "<" on " AND Px.NAME_FULL_FORMATTED < P.NAME_FULL_FORMATTED"?
0
 
Brendt HessSenior DBACommented:
The check on name was primarily because you showed that the name could differ.  If two entries happen on the same day, and only date (not time) is chosen, we need some way to differentiate between the two, e.g.:

ABC  1234567890  01/01/2005  John Doe DBA
ABC  1234567890  01/01/2005  Jane Doe DBA
 
  I arbitrarily chose name, but there may be more appropriate fields.  Alternately, it may not be possible to enter two records on the same day for different names, in which case this is unneeded.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now