Solved

SQL Query

Posted on 2014-03-12
1
116 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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

11 Experts available now in Live!

Get 1:1 Help Now