“'select to_char(application_date :: timestamp,'”

时间:2019-03-26 07:17:00

标签: postgresql

EXECUTE 'select to_char(application_date::timestamp, 'Mon-YY') as appl_month from my_schema.my_table;';

上面的PostgreSQL EXECUTE语句给出以下错误:

  

错误:“'select或附近的语法错误   to_char(application_date :: timestamp,'“ LINE 1:EXECUTE'选择   to_char(application_date :: timestamp,'Mon-YY ...                   ^

     

**********错误**********

     

错误:“'select或附近的语法错误   to_char(application_date :: timestamp,'“ SQL状态:42601字符:9

任何建议都会有所帮助。

更改为以下声明

EXECUTE 'select to_char(application_date::timestamp, ' || quote_literal(Mon-YY) || ') from standard.npo_weekly_export;';

但是给出了新的错误:

ERROR:  syntax error at or near "'select to_char(application_date::timestamp, '"
LINE 1: EXECUTE 'select to_char(application_date::timestamp, ' || qu...
                ^
********** Error **********

ERROR: syntax error at or near "'select to_char(application_date::timestamp, '"
SQL state: 42601
Character: 9

预期的输出:-以Mon-YY格式按月计数

Application month   Application #   Final Approval #    
Jan-17              1,000           800
Feb-17              1,010           808
Mar-17              1,020           816
Apr-17              1,030           824

如果我执行以下查询:

select to_char(application_date, 'Mon-YY') as appl_month, 
    count(distinct application_id) as appl_count, 
    sum(final_approval_ind) as fa_count,
from my_schema.my_table 
group by appl_month 
order by appl_month;

生成的输出:(注意:按文本而不是按日期排序)

"Apr-17";94374;19953
"Apr-18";87446;20903
"Aug-17";102043;21536
"Aug-18";91107;20386
"Dec-17";63263;13755
"Dec-18";21358;74
"Feb-17";89447;18084
"Feb-18";75426;16144
"Jan-17";86103;16394
"Jan-18";79403;17766
"Jul-17";90380;18929
"Jul-18";85439;20186
"Jun-17";95596;20403
"Jun-18";85764;18707
"Mar-17";112929;23323
"Mar-18";91179;21841
"May-17";101907;22349
"May-18";90885;21550
"Nov-17";78284;16791
"Nov-18";80472;7656
"Oct-17";87955;18524
"Oct-18";82821;17056
"Sep-17";80740;17788
"Sep-18";75785;18009

问题: to_char()返回文本,它按文本而不是按日期排序。因此输出混乱,而不是按Mon-YY排序。

1 个答案:

答案 0 :(得分:1)

在保留数据类型的派生表(也称为“子查询”)中进行聚合,然后在外部查询中进行排序:

select to_char(ap_month, 'Mon-YY') as appl_month
       appl_count, 
       fa_count
from (       
  select date_trunc('month', application_date) as ap_month, 
         count(distinct application_id) as appl_count, 
         sum(final_approval_ind) as fa_count,
  from my_schema.my_table 
  group by ap_month
) t
order by ap_month;

date_trunc('month', application_date)会将application_date规范化为月初,但将保留date数据类型,以便外部查询中的排序正常进行。

我不知道问题中的动态SQL应该做什么,但是如果出于某种原因(例如出于动态SQL的原因)需要使用该查询,则需要通过加倍对单引号进行转义。

execute '
  select to_char(ap_month, ''Mon-YY'') as appl_month
         appl_count, 
         fa_count
  from (       
    select date_trunc(''month'', application_date) as ap_month, 
           count(distinct application_id) as appl_count, 
           sum(final_approval_ind) as fa_count,
    from my_schema.my_table 
    group by ap_month
  ) t
  order by ap_month;
'; -- end of dynamic SQL

但是使用Postgres的dollar quoting会更容易:

execute $dyn$
  select to_char(ap_month, 'Mon-YY') as appl_month
         appl_count, 
         fa_count
  from (       
    select date_trunc('month', application_date) as ap_month, 
           count(distinct application_id) as appl_count, 
           sum(final_approval_ind) as fa_count,
    from my_schema.my_table 
    group by ap_month
  ) t
  order by ap_month;
$dyn$; -- end of dynamic SQL

请注意,您可以嵌套带引号的字符串,因此,如果在函数内部使用该查询,只需使用与函数主体不同的定界符(请参见example in the manual