Solved

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

Posted on 2016-10-18
4
50 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL GROUP BY 6 82
Display field if column exists 7 35
sql select total by week ending 3 26
TSQL Assignining CTE column in to a variable 3 13
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

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