1

When I try to run this SELECT statement, I get this error: ORA-00904: "GPASCORE": invalid identifier. How do I use the result of the gpaScore CASE statement in the WHERE clause?

SELECT
  (CASE grade
   WHEN 'A+'
     THEN 4
   WHEN 'A'
     THEN 4
   WHEN 'A-'
     THEN 3.7
   WHEN 'B+'
     THEN 3.3
   WHEN 'B'
     THEN 3
   WHEN 'B-'
     THEN 2.7
   WHEN 'C+'
     THEN 2.3
   WHEN 'C'
     THEN 2
   WHEN 'C-'
     THEN 1.7
   WHEN 'D+'
     THEN 1.3
   WHEN 'D'
     THEN 1
   WHEN 'D-'
     THEN 0.7
   WHEN 'F'
     THEN 0
   ELSE NULL END)   gpaScore
FROM PGFINALGRADES
WHERE gpaScore = 3
  • Unrelated, but: the parentheses around the CASE .. END are completely unnecessary. – a_horse_with_no_name Feb 25 '16 at 22:24
3

The problem is that Oracle evaluates the SELECT after the WHERE clause.

You can either put your query in a subselect:

SELECT gpaScore
FROM   (SELECT ( CASE grade
                   WHEN 'A+' THEN 4
                   WHEN 'A' THEN 4
                   WHEN 'A-' THEN 3.7
                   WHEN 'B+' THEN 3.3
                   WHEN 'B' THEN 3
                   WHEN 'B-' THEN 2.7
                   WHEN 'C+' THEN 2.3
                   WHEN 'C' THEN 2
                   WHEN 'C-' THEN 1.7
                   WHEN 'D+' THEN 1.3
                   WHEN 'D' THEN 1
                   WHEN 'D-' THEN 0.7
                   WHEN 'F' THEN 0
                   ELSE NULL
                 END ) gpaScore
        FROM   PGFINALGRADES) b
WHERE  gpaScore = '3'; 

Or, you could replicate the CASE statement in the WHERE clause:

SELECT ( CASE grade
           WHEN 'A+' THEN 4
           WHEN 'A' THEN 4
           WHEN 'A-' THEN 3.7
           WHEN 'B+' THEN 3.3
           WHEN 'B' THEN 3
           WHEN 'B-' THEN 2.7
           WHEN 'C+' THEN 2.3
           WHEN 'C' THEN 2
           WHEN 'C-' THEN 1.7
           WHEN 'D+' THEN 1.3
           WHEN 'D' THEN 1
           WHEN 'D-' THEN 0.7
           WHEN 'F' THEN 0
           ELSE NULL
         END ) gpaScore
FROM   PGFINALGRADES
WHERE ( CASE grade
           WHEN 'A+' THEN 4
           WHEN 'A' THEN 4
           WHEN 'A-' THEN 3.7
           WHEN 'B+' THEN 3.3
           WHEN 'B' THEN 3
           WHEN 'B-' THEN 2.7
           WHEN 'C+' THEN 2.3
           WHEN 'C' THEN 2
           WHEN 'C-' THEN 1.7
           WHEN 'D+' THEN 1.3
           WHEN 'D' THEN 1
           WHEN 'D-' THEN 0.7
           WHEN 'F' THEN 0
           ELSE NULL
         END ) = '3';

Or, if your real case is about as simple as this, you can put grade in your WHERE clause:

SELECT ( CASE grade
           WHEN 'A+' THEN 4
           WHEN 'A' THEN 4
           WHEN 'A-' THEN 3.7
           WHEN 'B+' THEN 3.3
           WHEN 'B' THEN 3
           WHEN 'B-' THEN 2.7
           WHEN 'C+' THEN 2.3
           WHEN 'C' THEN 2
           WHEN 'C-' THEN 1.7
           WHEN 'D+' THEN 1.3
           WHEN 'D' THEN 1
           WHEN 'D-' THEN 0.7
           WHEN 'F' THEN 0
           ELSE NULL
         END ) gpaScore
FROM   PGFINALGRADES
WHERE grade = 'B'
|improve this answer|||||
3

You can not use a column alias in the where clause. You can do the following: use your original query as a subquery.

SELECT * FROM (
SELECT
  (CASE grade
   WHEN 'A+'
     THEN 4
   WHEN 'A'
     THEN 4
   WHEN 'A-'
     THEN 3.7
   WHEN 'B+'
     THEN 3.3
   WHEN 'B'
     THEN 3
   WHEN 'B-'
     THEN 2.7
   WHEN 'C+'
     THEN 2.3
   WHEN 'C'
     THEN 2
   WHEN 'C-'
     THEN 1.7
   WHEN 'D+'
     THEN 1.3
   WHEN 'D'
     THEN 1
   WHEN 'D-'
     THEN 0.7
   WHEN 'F'
     THEN 0
   ELSE NULL END)   gpaScore
FROM PGFINALGRADES
)
WHERE gpaScore = '3';
|improve this answer|||||
1

As well as Balázs' solution, you can also do nifty stuff with CTEs (Common Table Expressions - aka WITH clause). Take a look at what I've done below and you should be able to adapt it. You can use the result of your SQL in any way you please. This code works on PostgreSQL (no running Oracle instance), but it should work on Oracle.

WITH CTE AS
(SELECT
  (CASE student_grade
   WHEN 'A+'
     THEN 4
   WHEN 'A'
     THEN 4
   WHEN 'A-'
     THEN 3.7
   WHEN 'B+'
     THEN 3.3
   WHEN 'B'
     THEN 3
   WHEN 'B-'
     THEN 2.7
   WHEN 'C+'
     THEN 2.3
   WHEN 'C'
     THEN 2
   WHEN 'C-'
     THEN 1.7
   WHEN 'D+'
     THEN 1.3
   WHEN 'D'
     THEN 1
   WHEN 'D-'
     THEN 0.7
   WHEN 'F'
     THEN 0
   ELSE NULL END)   gpaScore
FROM grade),
CTE2 AS
(SELECT gpaScore + 6 FROM CTE)  -- added 6 to my scores, just for the fun of it!
INSERT INTO mytest (SELECT * FROM CTE2);

Now, I select from test table (I only used a few sample records):

SELECT * FROM mytest;

gpa_plus
10
10
9.3
9
8
8.3
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.