?
Solved

SQL JOINS in Access

Posted on 2008-06-23
3
Medium Priority
?
204 Views
Last Modified: 2013-11-05
I have a small SQL query. This is the SQL at the moment:

"SELECT tblClass.ClassID, tblClass.Subject, tblClass.Teacher,
sum(iif (datepart('ww',tblHomework.DateSet)=(datepart('ww',Date())) ,1,0)) as wk1,
sum(iif (datepart('ww',tblHomework.DateSet)=(datepart('ww',Date())-1) ,1,0)) as wk2,
sum(iif (datepart('ww',tblHomework.DateSet)=(datepart('ww',Date())-2) ,1,0)) as wk3,
sum(iif (datepart('ww',tblHomework.DateSet)=(datepart('ww',Date())-3) ,1,0)) as wk4
FROM tblClass LEFT JOIN tblHomework ON tblClass.ClassID = tblHomework.ClassID
WHERE (tblHomework.TaskType = 'HW' AND Subject='" & Request("Faculty") & "')
GROUP BY tblClass.ClassID, tblClass.Subject, tblClass.Teacher"

It works just fine except for one thing. tblClass contains all the classes in the school. I want all the classes to show even if they are not found in tblHomework. Basically the SQL should count the number of homeworks set for each class. If the class does not appear in tblHomework that would imply no homework has been set.

I have fiddled about with RIGHT JOIN and LEFT join but the output is the same. It only shows the classes that can be matched in both tables. I am using Access so FULL OUTER JOIN is not acceptable.

Any ideas how to solve this one?

Thanks,

Desi
0
Comment
Question by:DesiMck
[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
3 Comments
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 500 total points
ID: 21848289
Your WHERE clause is limiting you to rows that match.

Try this:

WHERE ((tblHomework.TaskType is NULL or tblHomework.TaskType = 'HW') AND (Subject IS NULL or Subject='" & Request("Faculty") & "'))

Open in new window

0
 
LVL 4

Expert Comment

by:msfletch
ID: 21848290
Desi,

I believe the problem is in your WHERE clause. Even thought you are LEFT joining the tables, your WHERE clause is requesting only records that contain tblHomework.TaskType = 'HW'. This will limit your results from the tblClass table.
0
 
LVL 1

Author Closing Comment

by:DesiMck
ID: 31469851
Thank you - that was very useful and well explained.  Cheers,  Desi
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 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