Solved

sub-query

Posted on 2006-07-21
3
361 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:Brendt Hess
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:
Brendt Hess 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

615 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