Trying to wrap my brain around self-joins.

Can't seem to understand the syntax or the logic underlying self-joins as seen in the code below:

SELECT e1.last_name |' working for ' | E2.last_name 'employees and their Managers.

E1, e2 employees employed

WHERE e1.manager_id = e2.employee_id

AND e1.last_name LIKE '% R '.

ORDER BY e1.last_name;

I understand all parts of this (I think) except this part: e1.manager_id = e2.employee_id

I've been manually through the employees table, and there is no line where the manager_id = employe_id. But, however, the results are Oracle are as below:

Employees and their managers

-------------------------------

RAJS works for Mourgos

Raphaely working for the King

Works of Rogers for Kaufling

Russell works for the King

What am I misunderstanding? Thank you!!

talk2me wrote:

So what you are saying is that:

e1.employee_id = e2.manager_id that says:

1st reference in the table employees (for example):

employee_ID is 125 and has a manager whose manager_ID is 120

...

...

All employees, whether they are responsible for or are not in the table 'employees '. Each person has a unique employe_id, whether or not he is Manager.

In the recording with employee_id = 125, the manager_id is 120. And 120 exists as a number in the employees table.

talk2me wrote:

...

Because now we know the manager_ID as 120, we now go to the 2nd reference in the employees table and look 120 employee_ID and know who is this person. Then, this person is 125 Manager employee_ID.

Is this correct thinking?

Yes, that's correct.

It would be clearer if you run queries individually while you run through the thought process that precedes.

Given that we have two copies (e1 and e2) from the same table 'employees', take a look at the following:

SQL>
SQL> -- Row from first copy of employees (e1)
SQL> select e1.employee_id, e1.manager_id, e1.last_name
  2    from hr.employees e1
  3   where e1.employee_id = 125;

EMPLOYEE_ID MANAGER_ID LAST_NAME
----------- ---------- -------------------------
  125   120 Nayer

1 row selected.

SQL>
SQL> -- Join condition is e1.manager_id = e2.employee_id
SQL> -- But e1.manager_id = 120 from the query above. Substitute it in the join condition.
SQL> -- So, now let's see what 120 = e2.employee_id returns.
SQL> -- We check the second copy of employees (e2)
SQL> select e2.employee_id, e2.manager_id, e2.last_name
  2    from hr.employees e2
  3   where e2.employee_id = 120;

EMPLOYEE_ID MANAGER_ID LAST_NAME
----------- ---------- -------------------------
  120   100 Weiss

1 row selected.

SQL>
SQL> --
SQL> -- Both the results in one line
SQL> --
SQL> SELECT e1.employee_id as e1_employee_id,
  2      e1.manager_id as e1_manager_id,
  3      e1.last_name as e1_last_name,
  4      --
  5      e2.employee_id as e2_employee_id,
  6      e2.manager_id as e2_manager_id,
  7      e2.last_name as e2_last_name,
  8      --
  9      e1.last_name||' works for '||e2.last_name "Employees and Their Managers"
10    FROM hr.employees e1, hr.employees e2
11  WHERE e1.manager_id = e2.employee_id
12    AND e1.employee_id = 125
13  ORDER BY e1.last_name;

E1_EMPLOYEE_ID E1_MANAGER_ID E1_LAST_NAME    E2_EMPLOYEE_ID E2_MANAGER_ID E2_LAST_NAME  Employees and Their Managers
-------------- ------------- --------------- -------------- ------------- --------------- -------------------------------------------------------------
    125 120 Nayer 120      100 Weiss  Nayer works for Weiss

1 row selected.

SQL>
SQL>

employee_id 125 ("Nayer") has manager_id = 120.

Return to the second copy of the employees and see who is 120.


Don't forget, all manager_ids are present in the employee_ids table.

Employee_id is unique.

120 can only happen once in the column "employe_id." But it can be repeated in the column "manager_id".

employee_id = 120 is "Weiss".

So, "Nayer" works for "Weiss".

Tags: Database

Similar Questions

  • Self-joins in Discoverer?

    Hello

    I'm new to the discoverer. According to the "All discoverer" page, I use the following:
    OracleBI Discoverer 10g (9.0.4.00.00)
    Oracle Business Intelligence Discoverer Plus 10g (10.1.2.55.26)
    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi

    I'm looking to create reports of discoverer of data requiring the self-joins, and I wonder if this is even possible.

    A simplified form of the query I use looks like the following:
    SELECT
            qll.operand
          , pa.list_header_id
          , pa.list_line_id
    
    FROM
            apps.qp_pricing_attributes        pa
          , apps.qp_pricing_attributes        pa1
          , apps.qp_pricing_attributes        pa2
          , apps.qp_list_lines                qll
    
    WHERE   qll.LIST_LINE_ID                  = pa.LIST_LINE_ID
    AND     pa.PRICING_ATTRIBUTE              = 'PRICING_ATTRIBUTE23'   -- ship dates
    
    AND     qll.list_line_id                  = pa1.list_line_id (+)
    AND     pa1.PRICING_ATTRIBUTE             = 'PRICING_ATTRIBUTE27'   -- origin
    
    AND     qll.list_line_id                  = pa2.list_line_id (+)
    AND     pa2.PRICING_ATTRIBUTE             = 'PRICING_ATTRIBUTE99'   -- item
    
    AND     pa.list_header_id                 =  12050      -- header id
    
    /******************************   TEST DATE   *********************************/
    AND     sysdate
                BETWEEN to_date(pa.PRICING_ATTR_VALUE_FROM,'YYYY/MM/DD HH24:MI:SS')
                AND     to_date(pa.PRICING_ATTR_VALUE_TO,  'YYYY/MM/DD HH24:MI:SS')
    Essentially, the above query returns price several attributes for a given rate and collapses them all to a single line of output.

    So are even possible in discoverer of self-joins? I note that in the documentation
    Oracle® Business Intelligence Discoverer Plus User Guide 10 g Release 2 (10.1.2.1) B13915-04
    the only reference to self-joins seems to be regarding the LAG function, although even that seems to offer very small overview of the use of this function.

    I guess I could ask a DBA to create a view and a report out of sight. But are there other options?

    THX.

    -Wayne

    In SQL * Plus, you can write the following SQL code
    Select a.empno, a.ename, b.empno, b.ename
    of the emp a, b of the emp
    where a.mgr = b.empno;

    In Discoverer, you cannot join EMP to itself.

    work around
    ========

    Import the EMP table in your sector of activity for
    a second time, giving you a table EMP1. Then, create a join between EMP and EMP1:

    EMP. MGR = EMP1. ENAME

  • Wrap the object around a path?

    Hello!

    I was wondering if there is a quick way to do it. Watch here: 2015-02-19_0920 - library of losse2010 I am trying to wrap the links in the chain along the "curly" of the text. At first, I thought that I cut and paste the link and then turn them to follow the line but realized it's a lot of your time! I was wondering if there is a way to align the links throughout the text 'lines' of faster way using a path or a brush any?

    THX

    What you want is a brush model.

    https://helpx.Adobe.com/Illustrator/using/brushes.html

    https://helpx.Adobe.com/Illustrator/how-to/Illustrator-create-pattern-brush.html

  • Need help with a self-join query

    Hello
    I have A table with the following data

    OID parent_oid
    4 of 10
    4 2
    2 2
    12 6
    6 6

    parent_oid is the parent of the oid. I would like a query that displays the final parent of the oid. The result must indicate the following

    Final parent OID
    2 of 10
    4 2
    2 2
    12 6
    6 6

    I use Oracle 10 g. I am familiar with free joins, but that alone will not do the job. Thank you!

    Hello

    arizona9952 wrote:
    ... I am familiar with free joins, but that alone will not do the job.

    You are absolutely right!

    A self-join 2-way would work for lines have no parent, or lines which are directly related to their final ancestor (such as the oid = 4), but not for what anyone further.
    A 3-way self-join would work to a level more away from the last row, but no more. That would be enough with the small set of sample data that you posted, but it won't work if you have added a new rank parent_id = 10.
    An N - way self-join would work for up to N + 1 levels, but no more.

    You need something that can go to any number of levels, such as CONNECT BY:

    SELECT     CONNECT_BY_ROOT oid     AS oid
    ,     parent_oid          AS final_parent
    FROM     a
    WHERE     CONNECT_BY_ISLEAF     = 1
    CONNECT BY     oid     = PRIOR parent_oid
         AND     oid     != parent_oid
    ;
    

    Published by: Frank Kulash, February 22, 2010 19:09

    On sober reflection, I think that a request from top down, as one below, would be more effective than a motion up and down, like the one above:

    SELECT     oid
    ,     CONNECT_BY_ROOT     parent_oid     AS final_parent
    FROM     a
    START WITH     parent_oid     = oid
    CONNECT BY     parent_oid     = PRIOR oid
         AND     oid          != PRIOR oid
    ;
    
  • Request Cross-listing (Partition by Clause? Self-join?)

    Hello

    I need a query that cross-list will be a teacher teaches this semester of courses. Essentially, both fields must be the same (i.e.: CourseTitle & Section), while the third field is different (for example: subject).

    For example, Max Power is a Professor teaching the 3 course, one co-inscrit (123 ENG and JRL 123):
    LastName     FirstName     Subject     Section     CourseTitle
    Power          Max          ENG     123     English Composition
    Power          Max          ENG     452     Robert Frost Poetry     
    Power          Max          JRL     123     English Composition
    Power           Max          ENG      300     Faulkner & Twain
    The result of the desired query is:
    LastName     FirstName     Subject     Section     CourseTitle
    Power          Max          ENG     123     English Composition
    Power          Max          JRL     123     English Composition
    Basically, I don't need that the courses co-inscrit in the output. Is this an instance where I use a "Partition by Clause' or should I create a self-join?

    Well thanks for all help and comments.

    Hello

    user11137889 wrote:
    Unfortunately, I can't create tables. I don't have permission. I can't modify, add or remove all the data.

    It is no possible to work! You should have your own schema in a database of development, perhaps on your own machine, where you can create tables freely. You can develop or test software effectively without this ability.
    You must post CREATE TABLE and INSERT to your sample data, even if you can't test. Without this, all anyone can do is guess.

    If you want to only count each topic once, then replace COUNT (*) by COUNT (DISTINCT subject). Here, 'subject' can be almost any kind of expression, for example:

    count( DISTINCT substr ( ssbsect_ptrm_code
                            , 1
                     , 1
                     )
         ) over ( partition by  ssbsect_crse_numb
                      ,          scbcrse_title
             ) cnt
    
  • Best self-join necessary.

    Is there a more effective way to get data at the bottom of the example I created? It is a completely false attempt to imitate what I really need.

    I need to get the registration information based on a query that uses a global fuction based on the value of a field. The data in the record I need have the MAX (due_date) and in the case below, it would be recording #7
    CREATE TABLE TEST_TABLE (  
    ORDER_ID               VARCHAR2(15 Byte),
    CUST_ID                VARCHAR2(15 Byte),
    DUE_DATE               DATE,
    PAID_DATE              DATE,
    NAME                   VARCHAR2(25 Byte),
    ADDRESS                VARCHAR2(20 Byte),
    CITY                   VARCHAR2(20 Byte),
    STATE                  VARCHAR2(20 Byte),
    ZIP                    VARCHAR2(10 Byte)
    ); 
    COMMIT;
    
    INSERT INTO TEST_TABLE VALUES (1,11,'09-JAN-09','11-JAN-09','John Smith','1234 Main St.','Orlando','FL','35555');
    INSERT INTO TEST_TABLE VALUES (2,12,'21-JUL-09','21-JUL-09','Sally Sue','12 Way.','Orlando','FL','35568');
    INSERT INTO TEST_TABLE VALUES (3,16,'15-APR-09','15-APR-09','Bill Walsh','4 Rogers Ave.','Orlando','FL','85563');
    INSERT INTO TEST_TABLE VALUES (4,11,'20-AUG-09','20-AUG-09','John Smith','1234 Main St.','Orlando','FL','35555');
    INSERT INTO TEST_TABLE VALUES (5,98,'04-OCT-09','04-OCT-09','Philip Rogers','990 S New St.','Orlando','FL','96584');
    INSERT INTO TEST_TABLE VALUES (6,45,'05-NOV-09','06-NOV-09','Jack Johnson','234 New Main South.','Orlando','FL','36544');
    INSERT INTO TEST_TABLE VALUES (7,11,'01-OCT-09','06-OCT-09','John Smith','1234 Main St.','Orlando','FL','35555');
    INSERT INTO TEST_TABLE VALUES (8,12,'01-OCT-09','04-OCT-09','Sally Sue','887 New Way Apt1234.','Orlando','FL','87965');
    INSERT INTO TEST_TABLE VALUES (9,76,'10-MAY-09','10-MAY-09','Mike Jackson','41 N Alt Way','Orlando','FL','21458');
    COMMIT;
    
    
    select 
         tt2.order_id,
         tt2.paid_date,
         tt2.due_date,
         tt2.name,
         tt2.zip
    from
        (
        select 
            cust_id,
            max(due_date) as d_date
        from TEST_TABLE tt 
            where cust_id = '11'
        group by cust_id
        )tt1
        join TEST_TABLE tt2 on tt1.cust_id = tt2.cust_id and d_date = tt2.due_date
    ORDER_ID PAID_DATE END_DATE NAME ZIP
    7-10/06/2009-10/01/2009 John Smith 35555

    These data are required as well as other MULTIPLE tables, so this is not the only join or the one self-join. Yet once, would not join only there are some with more than 9 tables with more than 4 DB so group by at the end could get quite complex.

    Thanks in advance.

    Just scan the table once. Analytical use

    select order_id, paid_date, due_date, name, zip
      from (
            select order_id, paid_date, due_date, name, zip, cust_id, max(due_date) over() due_date_max
              from test_table
             where cust_id = '11'
           )
     where due_date = due_date_max
    
  • Reg: self-join n subqry.

    Hi friends
    can you tell me, in void query and Self join query will be faster and gives better performance.
    Example:

    Select * from emp where a.empid in (select empid from deparment where deptid = 10);
    OR
    Select * from emp a, Department b where a.empid = b.empid and deptid = 10'

    Who will be the best. Please suggest.

    -Best regards,.
    Aude - Darling

    you will understand by yourself if you check the plan to explain to the two statements...
    If not, please post them to the above statements

    Ravi Kumar

  • Trying to wrap text around an image in Dreamweaver CC2015.

    If I click on an image of the property bar rises, but in older versions by clicking on an image shows the bar properties with additional choices: spacing vertically and horizontally and "Align" with text wrap as a choice. Is now gone?  I read the other posts, but they seem to require coding rather than this quick method in design mode.  Or am I missing something?  Thank you.

    Is now gone?

    Yes.  Panels that has promoted old coding methods have disappeared.

    Dreamweaver is now much more web standards aware helps you produce better code.

    Use CSS floats, margins, and padding on your image.

    .floatLt {

    float: left;

    Padding: 1%;

    margin: 2%;

    border: 1px solid #EEE;

    }

    describe it

    This text will wrap around the floating element. This text will wrap around the floating element. This text will wrap around the floating element. This text will wrap around the floating element. This text will wrap around the floating element. This text will wrap around the floating element.

    Nancy O.

  • Text wrapping is square around a png image

    Hi I have a png as a file below.

    Sisters-of-St-John-of-God-Logo.png

    I know how to make a wrap text around an image, but the text surrounds the image as a rectangle

    Screen Shot 2016-06-04 at 7.14.15 PM.png

    and I want to wrap around the image, not the rectangle.  I tried to add a white background for the png and cutting the edges, see screenshot below.  I used red to display purposes.

    Sisters-of-St-John-of-God-Logo-cut-out.png

    But she always wraps as a rectangle.

    Appreciate your help.

    Screen Shot 2016-06-04 at 7.23.20 PM.png

    This works! When the PNG is incorporated.

  • How do wrap you text around images?

    I am trying to learn CSS, and I can't understand how to wrap text around pictures...

    http://aldenmarin.com/BTP/btpindex.htm

    My images are in columns. Also, what is the best way to adjust margins inside a column?

    Apply a rule to float left or float right to your image and include all desired margins

    {.imgfloatleft}

    float: left;

    margin-bottom: 0;

    right margin: 20px;

    }

  • Wrapping a design around a 3D object

    Hello, I am trying to understand how take the fish scale model that I did (on ovals) and processing it so that it is not only the form of the three sections of the sketch, but also that the scales remain aligned between them once the Blue design is wrapped around an object (3D) real.

    Ni17Scales.jpg

    I'm doing a sticker for all the world a model airplane.  That's what looked like the real plane:

    Nieuport_FlyingFish.jpg

    flying_fish_profile.jpg

    I intend hand painting the rest of the pieces of fish, but the 1/144 scale (about 25mm long), fish scales are a little small to paint evenly!

    If someone wants to play with the real .ai file, I downloaded on my server:

    www.wingsofwar.org/UserImages/FuselageWrap.ai

    Thank you for your time and help,

    Keith

    Here is a copy of your original file.

    JET

  • Could not find the wrapping of text around an image in Keynote

    I see post of 2007 where you can't wrap text around an image in Keynote. Is this always the case? Hope not... very disappointing if so.

    There is no tool to wrap text in Keynote. Text wrapping is a tool of print media to maximize space in the column in print, if that's what you try to do, use the Apple's Pages.

    Keynote is a presentation of screen based application, (slide show) so there is very little use for the text wrapping.

    You can do a work around, but it is not perfect:

    • Place the image on the slide

    • Place the two shapes on the slide, one for the area of text, image size
    • order click on the two forms;     to select both
    • Format > forms > subtract shapes;     will cut a hole in the greatest shape
    • Paste the text into the larger shape

    • format text

  • I want to wrap the text around a transparent sphere

    I want to be able to wrap text around a transparent in illustrator sphere, such as the text of Forefront is darker than the background text.  I followed this tutorial logo 3D (new) - YouTube video, but I can't understand what is happening after expansion to 06:13.  Can someone help me, please and thank you.

    Assuming that you got to the part where the object is expanded (06:10), depending on whether you choose the "direct selection tool" and select only the outline of the circle (invisible, but if you wanted to see go to view > outline) carefully not choose anything else and delete (twice). And then you just object > Dissocier 3 times. as a result your forehead back text will be selectable.

  • Unable to retrieve the data using self join

    I'm trying to find the time required to close a ticket. The ticket goes through different stages: NEW, INPROCESS, CLOSED, REOPENED. If the ticket is reopened so the next step would be INPROCESS and then CLOSED.

    The CC_TICKET_INFO table contains information on the last step of the ticket. Table CC_TICKET_HISTORY contains information on every step of the ticket.

    The challenge here is that if the ticket is reopened so it should be considered as 2 instances instead of 1.

    Trial of stages: NEW-INPROCESS-> CLOSE >

    Second instance of stages: REOPEND -> INPROCESS-> CLOSE.

    Following SQL is to generate the data:

    CREATE TABLE CC_TICKET_INFO

    (

    TICKET_ID VARCHAR2 (20 BYTE) NOT NULL

    TICKET_STATUS VARCHAR2 (60 BYTE)

    , created_date timestamp (6)

    , LAST_CHANGED timestamp (6)

    , ASSIGNED_TO VARCHAR2 (20)

    CONSTRAINT PK_CC_TICKET_INFO PRIMARY KEY

    (

    ticket_id

    )

    )

    CREATE TABLE CC_TICKET_HISTORY

    (

    TICKET_ID VARCHAR2 (20 BYTE) NOT NULL

    TICKET_STATUS VARCHAR2 (60 BYTE)

    , CREATED_DATE TIMESTAMP (6) NOT NULL

    ASSIGNED_TO VARCHAR2 (255 BYTE)

    CREATED_BY VARCHAR2 (60 BYTE)

    CONSTRAINT PK_CC_TICKET_HISTORY PRIMARY KEY

    (

    TICKET_ID

    CREATED_DATE

    )

    )

    insert into cc_ticket_history values ('D21207155', 'NEW', 6/28/2013-17:28:59 ', null, 'jsg - st');

    insert into cc_ticket_history values ('D21207155', 'INPROCESS', 6/28/2013-17:48:19 ', 'ah-eg","ah-eg");

    insert into cc_ticket_history values ('D21207155', 'CLOSED', 6/28/2013 18:54:23 ', 'ah-eg","ah-eg");

    insert into cc_ticket_history values ('D21207155', 'REOPENED', 7/2/2013 19:55:04 ', 'ah-eg","jsg - st");

    insert into cc_ticket_history values ('D21207155', 'INPROCESS', 7/2/2013 20:11:17 ', "sr - eg", "sr - eg");

    insert into cc_ticket_history values ('D21207155', 'CLOSED', 7/2/2013 23:06:16 ', "sr - eg", "sr - eg");

    insert into CC_TICKET_INFO values ('D21207155', 'CLOSED', 6/28/2013-17:28:59 ', ' 02/07/2013-23:06:16 ', "sr - eg");

    ______________________________________________________________________

    I want to find the time difference between 6/28/2013 18:54:23 'and 6/28/2013-17:28:59 ' (first instance)

    and between 23:06:16 7/2/2013 ' and 7/2/2013 19:55:04 "(second instance)

    Is the closest, I make the through this query:

    Select L.ticket_id, L.CREATED_DATE, R.CREATED_DATE as close_date, L.TICKET_STATUS, R.TICKET_STATUS cc_ticket_history cc_ticket_history join RIGHT L R on (L.rowid < R.rowid) where (L.TICKET_STATUS = 'NEW' R.TICKET_STATUS AND 'CLOSED' =) OR (L.TICKET_STATUS = 'REOPENED' AND R.TICKET_STATUS = 'CLOSED');

    Can it be done through SQL?

    Hello

    ORA - aff wrote:

    Etbin thanks for the reply. I think you're closer!

    The first 3 orders insert above form the first instance. The second 3 inserts statements form the second instance.

    Ideally, I'd like the output to something like this (values taken by top of insert statements):

    Ticket_ID STATUS CREATED_DATE CLOSED_DATE TIME_DIFFERENCE

    D21207155 CLOSED ON 28/06/2013 17:28:59 6/28/2013 18:54:23

    D21207155 CLOSED ON 07/02/2013 19:55:04 07/02/2013 23:06:16

    You can do this by using the analytical LAST_VALUE function:

    WITH got_opened_date AS

    (

    SELECT ticket_id, ticket_status, created_date

    LAST_VALUE (CASE

    WHEN ticket_status IN ('NEW', 'REOPENED')

    THEN created_date

    END

    IGNORES NULL VALUES

    ) OVER (PARTITION BY ticket_id

    ORDER BY created_date

    ) AS opened_date

    OF cc_ticket_history

    )

    SELECT ticket_id

    ticket_status AS status

    opened_date AS created_date

    created_date AS closed_date

    NULL AS time_difference

    OF got_opened_date

    WHERE ticket_status = 'CLOSED '.

    ORDER BY ticket_id

    closed_date

    ;

  • Seen the lock of brain on Left Join

    Always on Oracle 11.2.0.1.0

    Examples of data
    CREATE TABLE theEmails (
    RID NUMBER(10) PRIMARY KEY,
    employee NUMBER(6),
    emailType VARCHAR2(4),
    emailAddress VARCHAR2(50),
    emailActive number(1)
    )
    ;
    INSERT INTO theEmails VALUES(778437,231509,'OTHR','[email protected]',0);
    INSERT INTO theEmails VALUES(847168,231509,'WRK1','[email protected]',0);
    INSERT INTO theEmails VALUES(784690,231509,'OTHR','[email protected]',1);
    INSERT INTO theEmails VALUES(1459884,239868,'OTHR','[email protected]',0);
    INSERT INTO theEmails VALUES(1485781,239868,'OTHR','[email protected]',1);
    INSERT INTO theEmails VALUES(1485782,239868,'WRK1','[email protected]',0);
    INSERT INTO theEmails VALUES(1501028,231509,'WRK1','[email protected]',1);
    INSERT INTO theEmails VALUES(1492288,239868,'WRK1','[email protected]',1);
    INSERT INTO theEmails VALUES(1456785,239865,'OTHR','[email protected]',0);
    INSERT INTO theEmails VALUES(1489166,239865,'WRK1','[email protected]',0);
    INSERT INTO theEmails VALUES(1482668,239865,'WRK1','[email protected]',0);
    INSERT INTO theEmails VALUES(1482667,239865,'OTHR','[email protected]',1);
    
    
    
    CREATE TABLE thePeople (
    employee NUMBER(6),
    recordNum NUMBER(2),
    lastName VARCHAR2(20),
    firstName VARCHAR2(20)
    )
    ;
    
    INSERT INTO thePeople VALUES(231509,0,'P','Michelle');
    INSERT INTO thePeople VALUES(239868,0,'K','Dan');
    INSERT INTO thePeople VALUES(239865,0,'T','Mary');
    So, here is my SQL that does not work well:
    SELECT p.employee, p.recordNum, p.lastName, p.firstName, e.emailaddress, e.emailactive, e.emailtype
    FROM thePeople p LEFT JOIN theemails e ON p.employee = e.employee
    WHERE (e.emailactive = 1)
          AND
          (e.emailtype = 'WRK1')
          AND
          (e.emailaddress LIKE '%school.edu')
    ;
    AND this:
    SELECT p.employee, p.recordNum, p.lastName, p.firstName, e.emailaddress, e.emailactive, e.emailtype
    FROM thePeople p LEFT JOIN theemails e ON p.employee = e.employee
    WHERE (e.emailactive = 1 OR e.emailactive IS NULL)
          AND
          (e.emailtype = 'WRK1' OR e.emailtype IS NULL)
          AND
          (e.emailaddress LIKE '%school.edu' OR e.emailaddress IS NULL)
    Both give me this:

    employee | record | lastName. firstName. emailAddress. emailActive | emailType
    231509 | 0 | P     | Michelle | [email protected] | 1. WRK1


    But what I need is the following:
    employee | record | lastName. firstName. emailAddress. emailActive | emailType
    239868 | 0 | K     | Dan |      |     |
    231509 | 0 | P     | Michelle | [email protected] | 1. WRK1
    239865 | 0 | T     | Mary |          |


    Email from Dan is not included, because his "wrk1" email does not stop it school.edu
    E-mail Mary is not included, because his 'wrk1' email is not active.
    However, I need to come back with their numbers of name registration and employee.


    Once it works, I'll add employee = number in the WHERE clause, because I only need to find a person both for the actual production.

    What I am doing wrong?

    Thank you!

    Michelle

    Hello

    WHERE is the filtering actually record the selection.

    In your case, you might want to include in your LEFT OUTER JOIN ON conditions:

    SELECT p.employee, p.recordnum, p.lastname, p.firstname
         , e.emailaddress, e.emailactive, e.emailtype
      FROM    thepeople p
           LEFT JOIN
              theemails e
           ON     p.employee = e.employee
              AND e.emailactive = 1
              AND e.emailtype = 'WRK1'
              AND e.emailaddress LIKE '%school.edu';
    

    If you put them in your JOIN conditions actually join Chronogram of table theemails that adapt to the conditions of membership.
    As you use LEFT [OUTER] JOIN unmatched chronogram thepeople table will be displayed even when the theemail table column appears with a NULL value in the output files

    I hope that I was able to unlock your brain. :-)

    Kind regards.
    Al

    Published by: Alberto Faenza on 25 October 2012 16:58
    Additional note added

Maybe you are looking for

  • Windows Installer does not work... Windows 7 Home Premium 64-bit

    Hello! I am unable to get the windows installer to run.  I get the message: "The Windows Installer Service could not be accessed.  This can occur if Windows Installer is not installed properly.  Contact your support team. » The Windows Installer prog

  • BlackBerry Smartphones enable JavaScript

    Can someone tell me how do I enable JavaScript on my 8130.  Thank you and have a nice day

  • CC vs items and payment plan

    I am interested in buying photoshop but I don't know if I should go with elements or CC-I have a little experience with elements 7 and the intended use is for hobby, and I want the best of the best, so I have no limits. Which should I use?I would als

  • Can I install LR on MAC and Windows by buying a license

    I use two computers, one is a PC and Mac on the other. What can I install LR on my two computers with one license or I have to buy two licenses. Thanks

  • ESX3.5 - does not recognize data warehouses

    HelloI encountered a problem with one of the two ESX servers that we have. One of them broke down and I had to reinstall ESX 3.5, but now I see is no longer my storage devices. I have three warehouses of data listed in the second server. I use a MD30