|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, November 21, 2012 3:42 AM
Points: 7,
Visits: 13
|
|
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 ID Folder Name 1 Folder1 2 Folder2 3 Folder3 4 Folder4 5 Folder5 6 Folder6 7 Folder7
Permissions (table structure and data) Folder id Folder name permission 1 Folder1 Read 2 Folder2 Admin 3 Folder3 None 4 Folder4 admin
my result should be as below table: Folder name permission Folder1 Read Folder2 Admin Folder3 None Folder4 admin Folder5 None Folder6 None Folder7 none
Thanks in Advance
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Today @ 10:57 AM
Points: 838,
Visits: 2,200
|
|
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
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, November 21, 2012 3:42 AM
Points: 7,
Visits: 13
|
|
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.
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Today @ 10:57 AM
Points: 838,
Visits: 2,200
|
|
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
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Thursday, June 13, 2013 11:20 AM
Points: 225,
Visits: 462
|
|
Jason's script should work. Did you run it as is or add to it? Post the actual script that you ran.
--------------------------------------------------------------- Mike Hahn - Future MCM 2025  Right way to ask for help!! http://www.sqlservercentral.com/articles/Best+Practices/61537/ I post so I can see my avatar  I want a personal webpage  I want to win the lotto  I want a gf like Tiffa
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, November 21, 2012 3:42 AM
Points: 7,
Visits: 13
|
|
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
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, November 21, 2012 3:42 AM
Points: 7,
Visits: 13
|
|
Hi,
Finally I got it...
Thanks everyone...
|
|
|
|