1. Get rid of all advertisements and get unlimited access to documents by upgrading to Premium Membership. Upgrade to Premium Now and also get a Premium Badge!

Sql to look up value within same table

Discussion in 'SQL PL/SQL' started by MikeYoung, Sep 17, 2018.

  1. MikeYoung

    MikeYoung Starter

    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    10
    Location:
    USA
    Hi,

    I have a table Name: RelatedColor:

    ID Acct_Name Color Related_ID
    232 John White 836
    130 Jason Yellow 152
    131 Justin Yellow 130
    282 Julie Green 232

    I want the return value to create a new column "Related_Color":

    ID Acct_Name Color Related_ID Related_Color
    232 John White 836 null
    130 Jason Yellow 152 null
    131 Justin Yellow 130 Yellow
    282 Julie Green 232 White

    I need help on SQL:

    Select a.*, a.Color as Related_Color
    From RelatedColor a left join RelatedColor b
    on a.ID = b.Related_ID

    Thanks,
    Mike
     
  2. zargon

    zargon Community Moderator Forum Guru

    Messages:
    2,635
    Likes Received:
    368
    Trophy Points:
    1,430
    Location:
    Aurora, CO
    Code (SQL):
      1  SELECT a.*, b.Color AS Related_Color
      2  FROM RelatedColor a  JOIN RelatedColor b
      3  ON a.related_id = b.id
      4* WHERE a.related_id = b.id
    BING @ orclpdb1 > /

        ID ACCT_NAME        COLOR             RELATED_ID RELATED_COLOR         RELATED_COLOR
    ---------- -------------------- -------------------- ---------- -------------------- --------------------
           282 Julie        Green                232              White
           131 Justin        Yellow                130              Yellow
     
     
  3. GAURANG PRAHLADBHAI BHATT

    GAURANG PRAHLADBHAI BHATT Starter

    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    10
    Location:
    Ahmedabad
    Unable to Login in PL SQL Error Shown you are not logged in.. kindly help
     
  4. zargon

    zargon Community Moderator Forum Guru

    Messages:
    2,635
    Likes Received:
    368
    Trophy Points:
    1,430
    Location:
    Aurora, CO
    With what error? Post the error number and text so we all know what you are seeing. Also it's wrong to post a NEW topic in an existing, unrelated thread.

    Post this in a NEW thread. This response will be deleted in 3 days.