SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Fetching data from two tables


Fetching data from two tables

Author
Message
sunil446 69306
sunil446 69306
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
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
Jason-299789
Jason-299789
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2115 Visits: 3232
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
sunil446 69306
sunil446 69306
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
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.
Jason-299789
Jason-299789
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2115 Visits: 3232
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
Want a cool Sig
Want a cool Sig
SSC-Addicted
SSC-Addicted (418 reputation)SSC-Addicted (418 reputation)SSC-Addicted (418 reputation)SSC-Addicted (418 reputation)SSC-Addicted (418 reputation)SSC-Addicted (418 reputation)SSC-Addicted (418 reputation)SSC-Addicted (418 reputation)

Group: General Forum Members
Points: 418 Visits: 705
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 Cool
I want to win the lotto :-D
I want a gf like Tiffa w00t Oh wait I'm married!:-D
sunil446 69306
sunil446 69306
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
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
sunil446 69306
sunil446 69306
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 13
Hi,

Finally I got it...

Thanks everyone...
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search