Solved

SQL Query

Posted on 2014-03-12
1
119 Views
Last Modified: 2014-04-01
I have the following tables.

Student

SequenceNumber   CurrentQulificationStandard   Status    Date
1                                   A-2000                                       Y            2012-03-18
2                                   A-2001                                        N         2013-08-19
3                                    B-3006                                      Y           2014-02-18
4                                    C-0009                                      Y            2014-12-09

StudentList

StudentId             Qualified_Standard
123                        A-2000
125                        B-3006
121                        A-2001
188                        B-3006
118                        B-3006

I need to find -  given student has reached the CurrentQulificationStandard    or not ?
* Current Qualification standard is B-3006  . Since the date is <=GETDATE() AND MAX(Sequencenumber)=3 and STATUS=Y

In this above list three of them reached the current qualification standard
118  ,  188  , 125

For achieving this I am using following query. Is this correct ? How do I optimize this ?

 SELECT count(*)    FROM   StudentList   WHERE  StudentId  =125 AND Qualified_Standard
IN (
        SELECT CurrentQulificationStandard    FROM student   WHERE status='Y' AND  date<=GETDATE() AND SequenceNumber  
        IN (SELECT MAX(SequenceNumber) FROM student    WHERE status='Y' AND date<=GETDATE() ))
       
Above query returns the count  value to 1 so he is qualified
 
But student 123 is not qualified . so it will return 0


      Is this correct ? How do I optimize this query ?
0
Comment
Question by:Varshini S
1 Comment
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39922961
let's give this a try to return "all" the students that are "qualified standard"
 SELECT sl.StudentId  
   FROM StudentList sl
   WHERE sl.Qualified_Standard = 
  ( SELECT TOP 1 st.CurrentQulificationStandard    
          FROM Student st
        WHERE st.status='Y' 
           AND st.date <= GETDATE()
        ORDER BY st.SequenceNumber   DESC
  )

Open in new window

0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

778 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