Solved

SQL Query - Need some help

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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…

747 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

10 Experts available now in Live!

Get 1:1 Help Now