Solved

SQL Query - Need some help

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

752 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