如何在select查询中使用function列

时间:2016-07-26 17:03:35

标签: amazon-redshift

select now() as t1,t1;
  

错误: - 错误:42703:列“t1”不存在

结果应该是:

select now() as t1,t1;

7/26/2016   7/26/2016

1 个答案:

答案 0 :(得分:1)

您的查询不起作用,因为您执行select的那一刻,列t1尚未定义。此外,在您使用Amazon Redshift标记了您的问题时,请注意,您无法使用now(),但可以使用getdate()

要解决您的问题,您可以复制now() / getdate()逻辑:

select getdate() as t1, getdate() as t1;

或者从子选择中使用一次:

select t1, t1 from (select getdate() as t1);

任何一个人都会给你:

              t1              |              t1
------------------------------+------------------------------
 2016-07-28 06:43:46.23357+00 | 2016-07-28 06:43:46.23357+00
(1 row)

如果您需要输出看起来与您在问题中所说的完全相同:

select
    t1
  , t1
from (
    select
        regexp_replace(
            to_char(CURRENT_DATE, 'MM/DD/YYYY') -- gives 07/26/2016
          , '0([0-9]{1}\/)'                     -- stores "7/" in $1
          , '$1'
        ) as t1
);

,并提供:

    t1     |    t1
-----------+-----------
 7/28/2016 | 7/28/2016
(1 row)
相关问题