SELF JOIN

  • I want to return the Region Name for a organization. The Org_Name of the Organization will be passed into the SP.  I know this can be done with a Self Join, but I am having a little difficulting writing the SQL for it.

    (Region A is the Region for Organization 1 & 2)

    Org_Key     Org_Name      Region_Key

        1             Region A               1

        2             Organization 1        1   

        3             Organization 2        1   

     

    Thanks

    fryere

  • You'd need a self join in this situation but wouldn't it be better to have the region in a separate lookup table? Would be uch simpler to access the data that way.

  • It would be easier for a region table in this example, but all organizations will need to be in the same table for other reasons.

     

    Thanks

    fryere

  • Select Y1.Org_Name, Y2.Org_Name from dbo.YourTable Y1 inner join dbo.YourTable Y2 on Y1.Region_Key = Y2.Org_Key

  • Not that it matters a lot but it will be friendlier if

    Select Y1.Org_Name, Y2.Org_Name as Region_Name from dbo.YourTable Y1 inner join dbo.YourTable Y2 on Y1.Region_Key = Y2.Org_Key


    * Noel

  • I guessed that he would consider doing this himself. Cause if he didn't I wouldn't want be working with him anyways . (not that it matters either).

  • Thanks for the responses.  I will put it use asap.

     

    fryere

  • HTH.

Viewing 8 posts - 1 through 7 (of 7 total)

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