Solved

SQL Join query

Posted on 2008-10-26
6
219 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now