Solved

Exclude records that match or many instances of a value from an oracle query

Posted on 2016-10-18
4
53 Views
Last Modified: 2016-11-06
Using the query below, is it possible to exclude all instances of a Student_Number if the total instances of 'F' in STOREDGRADES.GRADE is greater than 1?  Thanks so much!!

SELECT     
STUDENT_NUMBER,
LAST_NAME,
FIRST_NAME,
STOREDGRADES.STORECODE, 
SECTIONS.TERMID, 
STOREDGRADES.COURSE_NAME, 
STOREDGRADES.COURSE_NUMBER, 
SECTIONS.SECTION_NUMBER, 
STOREDGRADES.TEACHER_NAME, 
STOREDGRADES.GRADE,  
STUDENTS.GRADE_LEVEL

FROM         
PS.STOREDGRADES STOREDGRADES
LEFT JOIN PS.STUDENTS STUDENTS ON STOREDGRADES.STUDENTID = STUDENTS.ID 
LEFT JOIN PS.SECTIONS SECTIONS ON SECTIONS.ID = STOREDGRADES.SECTIONID
LEFT JOIN PS.COURSES COURSES ON STOREDGRADES.COURSE_NUMBER = COURSES.COURSE_NUMBER

WHERE     
(STOREDGRADES.TERMID >= 2500) 
AND (STOREDGRADES.TERMID <= 2599) 
AND (STOREDGRADES.STORECODE IN ('Y1')) 
AND (STUDENTS.SCHOOLID IN (61)) 
AND (STUDENTS.GRADE_LEVEL = 10)
AND (STUDENTS.ENROLL_STATUS = 0)
AND (STOREDGRADES.GRADE IS NOT NULL)

Open in new window

0
Comment
Question by:Basssque
[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 74

Accepted Solution

by:
sdstuber earned 500 total points (awarded by participants)
ID: 41848641
SELECT student_number,
       last_name,
       first_name,
       storecode,
       termid,
       course_name,
       course_number,
       section_number,
       teacher_name,
       grade,
       grade_level
  FROM (SELECT student_number,
               last_name,
               first_name,
               storedgrades.storecode,
               sections.termid,
               storedgrades.course_name,
               storedgrades.course_number,
               sections.section_number,
               storedgrades.teacher_name,
               storedgrades.grade,
               students.grade_level,
               COUNT(CASE WHEN storedgrades.grade = 'F' THEN 1 END)
                   OVER (PARTITION BY student_number)
                   student_f_cnt
          FROM ps.storedgrades storedgrades
               LEFT JOIN ps.students students ON storedgrades.studentid = students.id
               LEFT JOIN ps.sections sections ON sections.id = storedgrades.sectionid
               LEFT JOIN ps.courses courses ON storedgrades.course_number = courses.course_number
         WHERE     (storedgrades.termid >= 2500)
               AND (storedgrades.termid <= 2599)
               AND (storedgrades.storecode IN ('Y1'))
               AND (students.schoolid IN (61))
               AND (students.grade_level = 10)
               AND (students.enroll_status = 0)
               AND (storedgrades.grade IS NOT NULL))
 WHERE student_f_cnt <= 1
0
 
LVL 32

Expert Comment

by:awking00
ID: 41848694
I'm not sure, but I suspect that student_name is not in the storedgrades table, so you may want to partition by storedgrades.student_id. (NO POINTS, PLEASE!)
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 41848774
the partitioning clause doesn't require all of the columns to be in the same table.

my guess is student id and student number can probably be used interchangeably.

however, if they can't,  the described requirement is to count Fs by student number, so that is what should drive the partitioning clause.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

726 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