Solved

Query two tables with out the same number of records on the second table.

Posted on 2011-09-12
6
200 Views
Last Modified: 2012-08-13
I have 2 tables, one with the columns

ClientForms
ClientFormID | Name | Description | FileName | ClientID
1                      Form1  Des1             f1.pdf           1901
2                      Form2  Des2             f2.pdf           1901
3                      Form3  Des3             f3.pdf           1901
4                      Form4  Des4             f4.pdf           1901
5                      Form5  Des5             f5.pdf           1901
6                      Form10  Des10         f10.pdf         1905
7                      Form11  Des11          f11.pdf         1905

and another table with the data saved for each form with the columns
FormsData
FormID | ClientID | ClientFormID | EmplID | Completed
1                1901              1                 2               Yes
2                1901              2                 2               Yes
3                1901              3                 3               Yes
4                1901              2                 3               Yes
5                1905              6                 4               Yes
6                1905              7                 4               Yes


I need to query both tables to show the forms belonging to the client for a specific employee and show whether or not the employee has completed the form. My problem is that id the employee has not yet completed the form, the record does not exist on the FormsData table.

The results I need to show if employe with id 2 and clientid 1901 are as follows

Name | Description | Completed
Form1   Des1                Yes
Form2   Des2                Yes
Form3   Des3                No
Form4   Des4                No
Form5   Des5                No

Thanks for the help in advance.
-Ben
0
Comment
Question by:Benjamin_
  • 2
  • 2
  • 2
6 Comments
 
LVL 32

Expert Comment

by:ewangoya
ID: 36526472
use left join
select name, description, ISNULL(completed, 'No') [Completed]
from ClientForms A
left join FormsData B on (A.ClientFormID = B.ClientFormID and A.ClientID = B.ClientID)

Open in new window

0
 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
ID: 36526479
or
select name, description, COALESCE(completed, 'No') [Completed]
from ClientForms A
left join FormsData B on (A.ClientFormID = B.ClientFormID and A.ClientID = B.ClientID)
where B.EmplID = 2

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36526508
If you use b.emplid in the where clause you are essentially doing an inner join, so you will want any criteria on second table to be un join criteria.
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36526640
In other words this:
select name, description, COALESCE(completed, 'No') [Completed]
from ClientForms A
left join FormsData B on (A.ClientFormID = B.ClientFormID and A.ClientID = B.ClientID)
where B.EmplID = 2

Is the same as this:
select name, description, COALESCE(completed, 'No') [Completed]
from ClientForms A
INNER join FormsData B on (A.ClientFormID = B.ClientFormID and A.ClientID = B.ClientID)
where B.EmplID = 2

When you probably mean:
select name, description, COALESCE(completed, 'No') [Completed]
from ClientForms A
left join FormsData B on (A.ClientFormID = B.ClientFormID and A.ClientID = B.ClientID) and B.EmplID = 2
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36526678
Thanks, Anthony. I was on mobile device earlier and had to keep the words sparse. Definitely needed a more clear example.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36526880
>>I was on mobile device earlier and had to keep the words sparse. <<
I figured as much.  I thought I would demostrate that occassionaly I can put more than two words together. :)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL Server creating a temp table 7 40
SQL Split character from numbers 3 18
Change part of a string 2 24
Test a query 23 19
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

743 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