Solved

SQL JOINS in Access

Posted on 2008-06-23
3
191 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

773 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