?
Solved

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

Posted on 2016-10-18
4
Medium Priority
?
56 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 2000 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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
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…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

800 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