Solved

SQL JOINS in Access

Posted on 2008-06-23
3
186 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you - that was very useful and well explained.  Cheers,  Desi
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

11 Experts available now in Live!

Get 1:1 Help Now