Solved

Unsure what SQL is needed for looking up cross tables.

Posted on 2008-10-28
5
191 Views
Last Modified: 2012-05-05
Afternoon all,

Two tables:

Students:
----------------------------
ID Name
0 Student1
1 Student2
2 Student3

StudentActive
----------------------------
StudentID IsActive
0 True
1 True
2 True
1 False

The studentactive table actually logs a history of student activity so there can be more than one student ID as shown with studentID 1.

How can I get a list of students who are active? In the above example, student1 and student3 are active but student 2 isn't. I'm doing this in VB but using Access 2007 as the database backend, but that shouldn't make much of a difference I wouldn't have thought.

Thanks,
Uni
0
Comment
Question by:Unimatrix_001
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:DigitalSnorkle
ID: 22823799
Does StudentActive have some kind of timestamp column?

You can't rely on elements remaining in input order.
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 22823951
There are a couple more columns in there so yes, but I haven't included them as they aren't too relevant.
0
 
LVL 5

Expert Comment

by:jfmador
ID: 22825554
This query will give you the status for your student for the last timestamp

SELECT s.ID, s.Active FROM Student s
INNER JOIN (
SELECT ID, Max(TimeStamp) as TimeStamp FROM students) t on s.ID = t.ID and s.TimeStamp = t.TimeStamp


0
 
LVL 5

Accepted Solution

by:
jfmador earned 500 total points
ID: 22825584
Sorry I miswritten several field

try this

SELECT Students.StudentID, Students.Name, s.Active
FROM (StudentActive s INNER JOIN (
SELECT StudentID, Max(TimeStamp) as TimeStamp FROM studentActive group by StudentID) t on s.StudentID = t.StudentID and s.TimeStamp = t.TimeStamp) INNER JOIN Students on s.StudentID = Students.ID
0
 
LVL 3

Author Closing Comment

by:Unimatrix_001
ID: 31510821
Thank you, :-)
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

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

14 Experts available now in Live!

Get 1:1 Help Now