Solved

sub-query

Posted on 2006-07-21
3
352 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
  • 2
3 Comments
 
LVL 32

Expert Comment

by:bhess1
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now