x
SELECT "public"."mv_tags_per_org"."count" AS "count", "public"."mv_tags_per_org"."tag_name" AS "Tag Name",
CASE
WHEN "public"."mv_tags_per_org"."ngo_id" = 30 then 'SSS'
WHEN "public"."mv_tags_per_org"."ngo_id" = 33 then 'PF'
WHEN "public"."mv_tags_per_org"."ngo_id" = 34 then 'DS'
ELSE 'Maybe'
END AS "NPO"
FROM "public"."mv_tags_per_org"
WHERE "NPO???" = "SSS"
Above you can see my code. It is currently returning exactly the output I want when you remove the “WHERE” function. I’m adding the “WHERE” function and attempting to access the new column I made called “NPO”. It seems as if the column does not exist to the SQL editor, but it does exist when the query is ran. How do I access it?
Thanks!
Advertisement
Answer
Enclose your query into a “table expression” so you can produce a named column. Then you can use it in the WHERE
clause:
select *
from ( -- table expression 'x' starts here
SELECT
"public"."mv_tags_per_org"."count" AS "count",
"public"."mv_tags_per_org"."tag_name" AS "Tag Name",
CASE
WHEN "public"."mv_tags_per_org"."ngo_id" = 30 then 'SSS'
WHEN "public"."mv_tags_per_org"."ngo_id" = 33 then 'PF'
WHEN "public"."mv_tags_per_org"."ngo_id" = 34 then 'DS'
ELSE 'Maybe'
END AS "NPO"
FROM "public"."mv_tags_per_org"
) x
WHERE "NPO" = 'SSS'
Note: “table expressions” are also called “derived tables” and “inline views” by different teams of people.