使用 "AS" 字段的条件查询
Conditional Query Using "AS" field
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"
上面你可以看到我的代码。当您删除 "WHERE" 函数时,它当前准确地返回我想要的输出。我正在添加 "WHERE" 函数并尝试访问我创建的名为 "NPO" 的新列。 SQL 编辑器似乎不存在该列,但当查询为 运行 时它确实存在。我如何访问它?
谢谢!
将您的查询包含在 "table expression" 中,这样您就可以生成命名列。然后就可以在WHERE
子句中使用了:
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'
注意:"table expressions"也被不同的团队称为"derived tables"和"inline views"。
WHERE
子句不能与 SELECT
子句中定义的列别名相关(因为前者在 之前 后者被评估)。
这对您的用例来说并不重要,可以简化为:
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 "public"."mv_tags_per_org"."ngo_id" = 30
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"
上面你可以看到我的代码。当您删除 "WHERE" 函数时,它当前准确地返回我想要的输出。我正在添加 "WHERE" 函数并尝试访问我创建的名为 "NPO" 的新列。 SQL 编辑器似乎不存在该列,但当查询为 运行 时它确实存在。我如何访问它?
谢谢!
将您的查询包含在 "table expression" 中,这样您就可以生成命名列。然后就可以在WHERE
子句中使用了:
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'
注意:"table expressions"也被不同的团队称为"derived tables"和"inline views"。
WHERE
子句不能与 SELECT
子句中定义的列别名相关(因为前者在 之前 后者被评估)。
这对您的用例来说并不重要,可以简化为:
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 "public"."mv_tags_per_org"."ngo_id" = 30