Self Join - Examples

  • Hi There,

    Please give some examples for more understanding on self join

    thanks

  • Just few examples:

    -- selecting orw with maximum date

    SELECT *

    FROM SomeTable st1

    JOIN (SELECT SomeID, MAX(SomeDateColumn) mxSomeDateColumn

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Just few examples:

    -- selecting row with maximum date:

    SELECT *

    FROM SomeTable st1

    JOIN (SELECT SomeID, MAX(SomeDateColumn) mxSomeDateColumn

    FROM SomeTable GROUP BY SomeID) st2

    ON st2.SomeID = st1.SomeID

    AND st2.mxSomeDateColumn = st1.SomeDateColumn

    -- selecting child rows from simple hierarchy table:

    SELECT st1.*

    FROM SomeTable st1

    JOIN SomeTable st2

    ON st2.SomeParentRecID = st1.RecID

    -- joining to itself on non-id column to check if another record with the same details

    SELECT st1.*

    FROM SomeTable st1

    JOIN SomeTable st2

    ON st2.SomeDetailColumn = st1.SomeDetailColumn

    WHERE st2.RecId != st1.RecId

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply