Using alias name in WHERE clause

让人想犯罪 __ 提交于 2019-11-28 14:27:58

You already know you can't use the alias in the where clause, but that only applies in the same level of SQL. You can wrap your query in an outer query:

SELECT *
FROM (
  SELECT pt.prod_desc AS"PROD_DESC",
  ...
   END)AS ".PRNT_PROD_DESC"
  FROM dims_prod_type pt
)
WHERE ".PRNT_PROD_ID" like 'A%';

The only alternative would be to repeat the whole case that generates that column in the where clause, which would be unpleasant with something so complicated.

Put the query in a subquery, then you can refer to the aliases in your where clause, e.g.:

SELECT * FROM (
  SELECT pt.prod_desc AS"PROD_DESC",
   ...etc...
  FROM dims_prod_type pt) pt
WHERE pt.".PRNT_PROD_ID" like 'A%';

Replace "PT".".PRNT_PROD_ID" by "PT"."PRNT_PROD_ID" or for a better understanding by pt.prnt_prod_id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!