Solved

SQL Query - Need some help

Posted on 2009-07-05
4
292 Views
Last Modified: 2012-05-07
Dear Experts,
I need help with querying a database.
I have two tables: Students and Studies.
Students - StudentID, FirstName, LastName
Studies - StudyID, BeginDate, EndDate, StudentID (FK)
The relation between the tables is a one(Students) to many(Studies). Basically, the 'studies' table describes the dates each student entered and left the School. He could have done that multiple times, so there could be multiple rows for each student.
Now I want to build a query that will return only the students who are currently in School. In order to do that, I need to get the most recent row that applies to that Student (based on the latest 'BeginDate'), and check if the current date is either between the BeginDate and EndDate or that the current date is after the BeginDate and the EndDate is NULL.
For example:
Students
STUDENTID   FIRSTNAME   LASTNAME
1                    John              Smith
2                    Mark              Clinton
3                    David             Bush
Studies
STUDYID   BEGINDATE   ENDDATE   STUDENTID
1                1/1/2008        1/1/2009        1
2                1/1/2010        NULL             1
3                1/1/2009        NULL             2

In such a case, I would only want to get 'Mark Clinton'. I wouldn't be interested in 'David Bush' because he has no entry in the Studies table.

So how do I write the query? (I'm using SQL Server Express)

Thank you so much!
0
Comment
Question by:PianoEn
  • 2
4 Comments
 
LVL 57

Assisted Solution

by:Raja Jegan R
Raja Jegan R earned 25 total points
ID: 24782981
Hope this helps:

And kindly SELECT only the required columns..
SELECT sd.*, st.*
FROM Students sd, Studies st
WHERE sd.STUDENTID = st.STUDENTID
AND st.ENDDATE IS NULL
AND st.BEGINDATE < getdate()

Open in new window

0
 
LVL 7

Expert Comment

by:Seo_Expert
ID: 24783241
Hello PianoEn,

As per you question below is the query.



SELECT sd.*, st.*
FROM stu sd, stuy st
WHERE sd.STUDENTID = st.STUDENTID
AND ((st.ENDDATE IS NULL
AND st.BEGINDATE > getdate()) or (st.BEGINDATE < getdate() AND st.ENDDATE > getdate()))

Open in new window

0
 
LVL 7

Accepted Solution

by:
Seo_Expert earned 100 total points
ID: 24783259
Previous comment I used my table names it is now correct.
SELECT sd.*, st.*
FROM Students sd, Studies st
WHERE sd.STUDENTID = st.STUDENTID
AND ((st.ENDDATE IS NULL AND st.BEGINDATE > getdate()) or 
(st.BEGINDATE < getdate() AND st.ENDDATE > getdate()))

Open in new window

0
 

Author Closing Comment

by:PianoEn
ID: 31600038
Thank you. I just changed the condition to "st.BEGINDATE < getdate()".
0

Featured Post

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

685 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