Solved

sub-query

Posted on 2006-07-21
3
360 Views
Last Modified: 2008-03-17
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
Comment
Question by:ethanjohnsons
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 32

Expert Comment

by:bhess1
ID: 17155287
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
 

Author Comment

by:ethanjohnsons
ID: 17209922
why do you put "<" on " AND Px.NAME_FULL_FORMATTED < P.NAME_FULL_FORMATTED"?
0
 
LVL 32

Accepted Solution

by:
bhess1 earned 500 total points
ID: 17217156
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question