Solved

SQL Query - Need some help

Posted on 2009-07-05
4
291 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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. …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

16 Experts available now in Live!

Get 1:1 Help Now