Fetching data from two tables

  • Hi All,

    I'm having 2 tables named "Folders" and "Permissions"

    I want my result to be all the folders from "Folders" table and permissions from "Permissions" table. if the folder does not exists in "Permissions" table then also folder should be appear in my result table with none permission

    Please help me writing query for this.

    Folder (table structure with data)

    Folder IDFolder Name

    1Folder1

    2Folder2

    3Folder3

    4Folder4

    5Folder5

    6Folder6

    7Folder7

    Permissions (table structure and data)

    Folder idFolder namepermission

    1Folder1Read

    2Folder2Admin

    3Folder3None

    4Folder4admin

    my result should be as below table:

    Folder namepermission

    Folder1Read

    Folder2Admin

    Folder3None

    Folder4admin

    Folder5None

    Folder6None

    Folder7none

    Thanks in Advance

  • You need to do an Outer Join, something like

    Select FolderName

    ,COALESCE(Permission,'None') Permission

    --,IsNull(Permission,'None') Permission --Alternate to COALESCE

    From Folder f

    LEFT OUTER JOIN [Permissions] p

    on f.FolderId=p.FolderId

    _________________________________________________________________________
    SSC Guide to Posting and Best Practices

  • Hi,

    Thanks for your reply I tried your query but i'm not getting the result as expected.

    I have 20 folder names in folders table and 15 folder names in permissions table.

    I'm still getting only 15 folder name rows which are equal to permissions table rows.

  • Curious,

    Create Table #folder (FolderId int, FolderName varchar(100))

    Create Table #Permissions (FolderId int, FolderName varchar(100),Permission varchar(100))

    Insert into #Folder

    Select 1 ,'Folder1'

    Union Select 2 ,'Folder2'

    Union Select 3 ,'Folder3'

    Union Select 4 ,'Folder4'

    Union Select 5 ,'Folder5'

    Union Select 6 ,'Folder6'

    Union Select 7 ,'Folder7'

    insert into #Permissions

    Select 1,'Folder1' ,'Read'

    Union Select 2 ,'Folder2' ,'Admin'

    Union Select 3 ,'Folder3' ,'None'

    Union Select 4 ,'Folder4' ,'admin'

    Select f.FolderName

    ,COALESCE(Permission,'None') Permission

    --,IsNull(Permission,'None') Permission --Alternate to COALESCE

    From #Folder f

    LEFT OUTER JOIN #Permissions p

    on f.FolderId=p.FolderId

    Heres my test rig with data you provided, and i get 7 rows returned.

    Try doing a Full outer Join, instead of the LEFT OUTER, that should pring back the missing on both sides.

    _________________________________________________________________________
    SSC Guide to Posting and Best Practices

  • Jason's script should work. Did you run it as is or add to it? Post the actual script that you ran.

    ---------------------------------------------------------------
    Mike Hahn - MCSomething someday:-)
    Right way to ask for help!!
    http://www.sqlservercentral.com/articles/Best+Practices/61537/
    I post so I can see my avatar :hehe:
    I want a personal webpage 😎
    I want to win the lotto 😀
    I want a gf like Tiffa :w00t: Oh wait I'm married!:-D

  • Hi,

    I just tried the script and it works. but I think there is some problem with my tables data so I'm not able to fetch records. let you know in short what my problem is. I tried same select query on my tables.

    Thank you

  • Hi,

    Finally I got it...

    Thanks everyone...

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

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