Solved

And/Or SQL Question

Posted on 2016-11-04
9
35 Views
Last Modified: 2016-11-23
I have a query where I'm trying to identify all the users who have a specific benefit plan.
Their dependents can also be part of this plan.

For the most part, I've properly identified all the users
The most active record in the database is where the ben.stop_date is 1/1/1700.
But I need to clean up the duplicates.

There are 2 issues causing the duplicates.
1.The duplicates are being caused by different values in the column dep_stop_date.
   This value should be 1/1/1700.
2. Not all users have dependents on the plan.
    Those users are being displayed, but a null value of 000000 is being displayed in the dep_stop_date column.

My question is how do I retrieve all the records without the duplicates and without omitting the users who do not have dependents on the plan?

Here is my code:

select  
                        e.employee,
                        e.emp_status,
                        b.start_date,
                        b.cov_option,
                        e.first_name,
                        e.fica_nbr,
                        e.last_name,
                        e.email_address,
                        e.addr1,
                        e.addr2,
                        e.city,
                        e.state,
                        e.zip,
                        p.birthdate,
                        p.hm_phone_nbr,
                        d.seq_nbr,
                        d.active_flag,
                        d.rel_code,
                        d.dep_type,
                        d.first_name dep_first_name,
                        d.last_name dep_last_name,
                        d.fica_nbr dep_fica_nbr,
                        d.birthdate dep_birthdate,                      
                        d.addr1 dep_addr1,
                        d.addr2 dep_addr2,
                        d.city dep_city,
                        d.state dep_state,
                        d.zip dep_zip,
                        d.hm_phone_nbr dep_hm_phone_nbr,
                        h.dependent dep_number,
                        h.plan_code dep_plan_code,
                        h.stop_date dep_stop_date,  
                        b.stop_date ben_stop_date  
                       
                  from  prod.employee e,
                        prod.paemployee p,
                        prod.emdepend d,  
                        prod.benefit b,
                        prod.hrdepben h
                  where  
                        e.employee=d.employee(+) AND  
                        e.company=d.company(+) AND
                        e.employee=p.employee(+) AND
                        e.company=p.company(+) AND
                        e.employee=b.employee(+) AND  
                        e.company=b.company(+) AND
                        H.COMPANY(+)=B.COMPANY AND
                        H.PLAN_TYPE(+)=B.PLAN_TYPE AND
                        H.EMPLOYEE(+)=B.EMPLOYEE AND
                        H.EMP_START(+)=B.START_DATE AND
                        H.PLAN_CODE(+)=B.PLAN_CODE AND
                        e.emp_status not like 'T%' AND  
                       (b.stop_date = TO_DATE ('01-01-1700 00:00:00', 'DD-MM-YYYY HH24:MI:SS'))
                       AND b.plan_code='IDWD'
                  order by e.employee,d.seq_nbr;
0
Comment
Question by:metalteck
[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
9 Comments
 
LVL 8

Expert Comment

by:Lee Ingalls
ID: 41874524
Have you tried Select Distinct? (synonymous with Select Unique).

Distinct is used in the Select statement to notify the query that you only want the unique items returned when a field holds data that may not be unique.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 41874577
you have contradicting conditions...

>>>  e.employee=b.employee(+) AND  

this forces an outer join (and other (+) joins on b.

>>>      AND b.plan_code='IDWD'

this forces the results to be not null, which is effectively negating the outer join, because only those rows returned by an inner join will satisfy criteria like that
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 41874583
after you fix your join conditions,  what do you want to do with duplicates?


if a given employee has 2 rows for 1/1/1700,  which one do you want returned?
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 32

Expert Comment

by:awking00
ID: 41874587
The experts here very likely know how to write such a query. What we don't know is your data. Can you provide some sample data (the relevant join and select fields are enough) for the five tables that encompass the various criteria you're having issues with and your desired results from that data? Please do so in a text format (i.e. not a picture) so we will have something we can test with.
0
 

Author Comment

by:metalteck
ID: 41874623
Completely understand your points and you can see where my dilemma is
IDWD is the plan that identifies all the users and is necessary.

I can remove the outer join on e.employee = b.employee.

I was thinking I can add a the following statements to the where clause to eliminate the duplicates:
(H.STOP_DATE = TO_DATE ('01-01-1700 00:00:00', 'DD-MM-YYYY HH24:MI:SS') or
                       h.stop_date is null)

But I get an error stating that outer join operators are not allowed for in/or .

Any suggestions on how I can resolve this?
0
 

Author Comment

by:metalteck
ID: 41874632
@sdstuber, the duplicates is occurring because the dep_stop_date has a value of 1/1/1700 on one line and an actual value on the next line.
I want to identify only those values without eliminating the records that have a null value
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points (awarded by participants)
ID: 41874633
use ANSI syntax instead of oracle's (+) syntax and you'll get support for more options

doing that and eliminating the outer join on B would look something like this...

  SELECT e.employee,
         e.emp_status,
         b.start_date,
         b.cov_option,
         e.first_name,
         e.fica_nbr,
         e.last_name,
         e.email_address,
         e.addr1,
         e.addr2,
         e.city,
         e.state,
         e.zip,
         p.birthdate,
         p.hm_phone_nbr,
         d.seq_nbr,
         d.active_flag,
         d.rel_code,
         d.dep_type,
         d.first_name dep_first_name,
         d.last_name  dep_last_name,
         d.fica_nbr   dep_fica_nbr,
         d.birthdate  dep_birthdate,
         d.addr1      dep_addr1,
         d.addr2      dep_addr2,
         d.city       dep_city,
         d.state      dep_state,
         d.zip        dep_zip,
         d.hm_phone_nbr dep_hm_phone_nbr,
         h.dependent  dep_number,
         h.plan_code  dep_plan_code,
         h.stop_date  dep_stop_date,
         b.stop_date  ben_stop_date
    FROM prod.employee e
         INNER JOIN prod.benefit b
             ON e.employee = b.employee
            AND e.company = b.company
            AND b.stop_date = TO_DATE('01-01-1700 00:00:00', 'DD-MM-YYYY HH24:MI:SS')
            AND b.plan_code = 'IDWD'
         LEFT OUTER JOIN prod.paemployee p ON e.employee = p.employee AND e.company = p.company
         LEFT OUTER JOIN prod.emdepend d ON e.employee = d.employee AND e.company = d.company
         LEFT OUTER JOIN prod.hrdepben h
             ON h.company = b.company
            AND h.plan_type = b.plan_type
            AND h.employee = b.employee
            AND h.emp_start = b.start_date
            AND h.plan_code = b.plan_code
   WHERE e.emp_status NOT LIKE 'T%'
ORDER BY e.employee, d.seq_nbr;

Open in new window

0
 

Author Comment

by:metalteck
ID: 41874650
Here is the requested text file.
idwg.csv
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query assistance 16 48
IIF in access query 19 43
MS SQL Conditional WHERE clause 3 38
sql query display the latest row 10 54
In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

696 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