Solved

Showing time that Isn't there.

Posted on 2008-09-30
15
181 Views
Last Modified: 2011-10-19
Ok i'll try to explain this and not leave out too many details. If I do please ask and I'll provide them. I'm sure there is a easy solution to this i just cant figure it out right now...here goes: In table A i have an ID (key) that matches an ID in table B. For any given week students check in and out of table B, this creates a record in table B that looks something like:
 
ID | StudentName | TimeIn | TimeOut

Ifi want to query table B for TimeIn and TimeOuts for all students that match a student in table A then,..no big deal:
SELECT
            ID, TimeIn, TimeOut
FROM
           Table A AS a
              INNER JOIN
           Table B AS b ON a.ID = b.ID
WHERE
            TimeIn >= A_dateTime
Now, here is the problem, how do I  show the students that did not have time. In other words I want to show all the students and for those that did not have time display a message like:
"NO TIME" or something along those lines. And yes this is pretty much a "punch clock" or a payroll type of thing. The thing is that in some cases I do get the "NO TIME" students to show up, but not all the time so I'm not getting all of the students to show up...
0
Comment
Question by:mikesExpertExchange
  • 7
  • 5
  • 2
  • +1
15 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 22611348
You need an outer join:


SELECT
            a.ID, b.TimeIn, b.TimeOut
FROM
           Table A AS a
              LEFT JOIN
           Table B AS b ON a.ID = b.ID
WHERE
            b.TimeIn >= A_dateTime
0
 
LVL 14

Expert Comment

by:ldunscombe
ID: 22611440
You probably need an Is Null criteria also to pick up the students who don't have any records in table b

Something like this perhaps,

SELECT TableA.Name, IIf([Time In]>=[Whatever time],[Time In],"No Time") AS [Time in Indicator]
FROM TableA LEFT JOIN Tableb ON TableA.id = TableB.id
WHERE (((TableB.[Time in])>=[Whatever time])) OR (((TableB.[Time in]) Is Null));


Leigh
0
 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 22611904
matthewspatrick: i tried the outer join and still can't collect them all....

dunscombe: i tried your suggestion as well and still don't have them all....is there some other information i can give / provide?
0
 
LVL 17

Expert Comment

by:Natchiket
ID: 22612206
May I suggest another option

SELECT
            a.ID, b.TimeIn, b.TimeOut
FROM
           Table A AS a
              LEFT JOIN
           (SELECT * FROM Table B WHERE TimeIn >= A_dateTime) b
ON a.ID = b.ID

There is something that irks me slightly, though.. Presumably table A contains the student details.  Why does table B have the student name in ?  Surely this should be a property of table A


           
0
 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 22613776
Natchiket: Yes, that is correct, student name is in table A. It does get written to table B when a record is inserted though. Are you saying that it shouldn't be written into table B when a record is created?
0
 
LVL 17

Expert Comment

by:Natchiket
ID: 22622070
well a seasoned database developer will tell you that since the name is already in A, having it in B also is redundant since the ID field in B indicates the record in A and hence the student.  Of course there *may* be some perfectly legitimate reason for having the name also in B, although it's not obvious from the given scenario.  Generally speaking duplicate information in databases is a recipe for trouble because, apart from the overhead of extra strorage space etc, there's the problem of keeping the data consistent i.e. 2 or more tables having to be updated if one of the students changes their name (or needs their name corrected).
0
 
LVL 14

Expert Comment

by:ldunscombe
ID: 22628906
There must be something else going on here that I am not getting or misunderstanding.  I tested my query as posted and it worked without problem so I am at a loss as to why it isn't working for you.  The fact that you have the student name in both tables shouldn't make a difference to the results of the query however as Natchiket suggests it is probably not an optimum design.

Is it possible to post a sample of your db that we can take a look at and maybe figure what is going wrong.

Leigh
0
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

 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 22631069
ldunscombe: yes, i'm attaching a sample of the database, hopefully this will help. i had to change the names of the innocent to protect them, but i also wanted to give you a week's worth of data so instead of thinking up a couple hundred original names i changed everyone to 'Test Name', if this gets confusing let me know and i'll work on a better sample...
Sample.mdb
0
 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 22631071
if it asks you for a password,..try test
0
 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 22631073
that's 'test'
0
 
LVL 14

Expert Comment

by:ldunscombe
ID: 22631476
This seems to work in your sample.

SELECT AthleteInfo.StudentID, AthleteInfo.FirstName, AthleteInfo.LastName, IIf([TimeIn]>=[Pick A Date],[TimeIn],"No Time") AS Tm INTO Temp
FROM AthleteInfo LEFT JOIN In_Out ON AthleteInfo.StudentID = In_Out.StudentID
WHERE (((In_Out.TimeIn)>[Pick a Date])) OR (((In_Out.TimeIn) Is Null));


Leigh
0
 
LVL 14

Expert Comment

by:ldunscombe
ID: 22631492
Another thing that you might want to fix.

In your Athlete Info table  your StudentID field should be indexed (NO DUPLICATES)

Leigh
0
 
LVL 14

Accepted Solution

by:
ldunscombe earned 500 total points
ID: 22631511
Sorry, Sorry

Scratch that query,

Thats the one I used to test that all of the Students were included.
This is the one you want.


SELECT AthleteInfo.StudentID, AthleteInfo.FirstName, AthleteInfo.LastName, IIf([TimeIn]>=[Pick A Date],[TimeIn],"No Time") AS Tm
FROM AthleteInfo LEFT JOIN In_Out ON AthleteInfo.StudentID = In_Out.StudentID
WHERE (((In_Out.TimeIn)>[Pick a Date] Or (In_Out.TimeIn) Is Null));

Leigh
0
 
LVL 1

Author Comment

by:mikesExpertExchange
ID: 22670921
ok that seemed to work for the "current" week. i'm having trouble getting all of the students to show, this time, for a previous week in the past (historical). I've attached a sample of the database along with a query (within the database queries) to demonstrate my problem. In this case "Missing Thisone" and "Missing Thisonetoo" do not show up as "NO TIME" and they should as they have no time entered for the period specified. I don't know why though....
HistoricSample.mdb
0
 
LVL 1

Author Closing Comment

by:mikesExpertExchange
ID: 31501835
for the problem of the current week this solution was great
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

760 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

18 Experts available now in Live!

Get 1:1 Help Now