Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Beginner Question in PostgreSQL concerning CASE, WHEN, THEN, END AS

I am following a video tutorial that tackles <kbd>CASE</kbd>, <kbd>WHEN</kbd>, and <kbd>END AS </kbd> queries. pgAdmin can find it with <kbd>SELECT</kbd> but when using the <kbd>CASE</kbd> it ceases to exist!

select film_id, title, description, release_year, length, rating,

CASE

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

WHEN language_id ‘1’ THEN ‘English’

WHEN language_id ‘2’ THEN ‘Italian’

WHEN language_id ‘3’ THEN ‘Japanese’

WHEN language_id ‘4’ THEN ‘Mandarin’

WHEN language_id ‘5’ THEN ‘French’

WHEN language_id ‘6’ THEN ‘German’

END AS language,

rental_duration
from film

It returns as:

ERROR: type "language_id" does not exist
LINE 3: WHEN language_id ‘1’ THEN ‘English’
^
SQL state: 42704
Character: 62

I mimicked the query in the tutorial and double checking if I made a typo, but it still doesn’t exist.

>Solution :

I believe you have a mistake in the condition:

Example:

elect film_id, title, description, release_year, length, rating,

CASE

WHEN language_id = 1 THEN ‘English’

WHEN language_id = 2 THEN ‘Italian’

WHEN language_id = 3 THEN ‘Japanese’
.
.
.

END AS language,

rental_duration from film

Another Example:

SELECT OrderID, Quantity,
CASE
WHEN Quantity > 30 THEN ‘The quantity is greater than 30’
WHEN Quantity = 30 THEN ‘The quantity is 30’
ELSE ‘The quantity is under 30’
END AS QuantityText
FROM OrderDetails;

Let me know if this works.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading