Solved

SQL Query - Need some help

Posted on 2009-07-05
4
296 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

636 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