Using alias name in WHERE clause

后端 未结 3 1918
感情败类
感情败类 2020-12-12 03:56

I am executing the below query and using alias name for all columns. I have named alias with a . since that is a requirement. now, I want to refer to the alias name directly

相关标签:
3条回答
  • 2020-12-12 04:00

    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.

    0 讨论(0)
  • 2020-12-12 04:14

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

    0 讨论(0)
  • 2020-12-12 04:25

    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%';
    
    0 讨论(0)
提交回复
热议问题