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
Solved

SQL JOINS in Access

Posted on 2008-06-23
3
195 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
3 Comments
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 125 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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 …
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

809 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