Solved

SQL Join query

Posted on 2008-10-26
6
242 Views
Last Modified: 2012-05-05
How do i get the required result from querying the two tables.

tblGroups
Group      R      Folder3
Group      RW      Folder1
Group      R      Folder2
Group      RW      Folder4
Group     R      Folder5

tblFolders
Folder      R      Folder2
Folder      R      Folder1
Folder     RW   Folder5

Result:
Group      R      Folder3
Group      RW      Folder4
Folder      R      Folder2
Folder      R      Folder1
Folder     RW   Folder5
0
Comment
Question by:byte1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22807664
If I am understanding you correctly you want:
+All values of folders table
+Values from groups table where folder name is not preset in folders table

SELECT *
FROM tblFolders
UNION ALL
SELECT *
FROM tblGroups g
WHERE NOT EXISTS (SELECT 1 FROM tblFolders f WHERE f.FolderNameColumn = g.FolderNameColumn)

Open in new window

0
 

Author Comment

by:byte1
ID: 22807684
I need values from both tables but Folder table takes precedence. if a row exists in tblgroups and tblfolders ,tblFolders takes precedence.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22807702
The above should do the trick as that is the same principal as you will get all values of tblFolders first and then only the values that are not in tblFolders which will give you all values of both tables with the common values labelled as Folder instead of Group.
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22807711
The above was for simplicity sake, you may want to type out the column names and alias them how you want to show in the report since you are combining tables the predefined column names may not make sense after UNION.


SELECT Col1 AS FolderType, Col2 AS FolderRights, FolderNameColumn AS FolderName
FROM tblFolders
UNION ALL
SELECT Col1, Col2, FolderNameColumn
FROM tblGroups g
WHERE NOT EXISTS (SELECT 1 FROM tblFolders f WHERE f.FolderNameColumn = g.FolderNameColumn)

Open in new window

0
 

Author Comment

by:byte1
ID: 22807716
Sorry i did not include column names, my bad. Thanks for the solution it worked like a charm.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22807723
Glad to help and no worries.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question