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
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.