Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

SQL Query - Need some help

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
PianoEn
Asked:
PianoEn
  • 2
2 Solutions
 
Raja Jegan RSQL Server DBA & ArchitectCommented:
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
 
Seo_ExpertCommented:
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
 
Seo_ExpertCommented:
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
 
PianoEnAuthor Commented:
Thank you. I just changed the condition to "st.BEGINDATE < getdate()".
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now