【问题标题】:pass parameters in crosstab query postgres在交叉表查询 postgres 中传递参数
【发布时间】:2017-02-02 14:07:23
【问题描述】:

如何在crosstab查询postgresql中传递参数。 请参考 postgresql 中的以下函数

create function sp_nextfollowup_count_byweek(_from timestamp without time zone,_to timestamp without time zone)
returns table(userid integer,username character varying,day1 bigint,day2 bigint,day3 bigint,day4 bigint,day5 bigint,day6 bigint,day7 bigint)as
$BODY$
BEGIN
return query
with cte as(
select * from crosstab($$
select  follow_up_by,next_follow_up_date,count(*) from sales_enquiry_follow_up where next_follow_up_date between _from and _to
group by follow_up_by,next_follow_up_date 
order by follow_up_by,next_follow_up_date$$,$$select date::timestamp without time zone
from generate_series(
  _from,
  _to,
  '1 day'::interval
) date$$)
as(id integer, dd bigint,dd1 bigint,dd2 bigint,dd3 bigint,dd4 bigint,dd5 bigint,dd6 bigint)
)
select cte.id,u.username,cte.dd,cte.dd1,cte.dd2,cte.dd3,cte.dd4,cte.dd5,cte.dd6 from cte left join users u on cte.id=u.id;

END;
$BODY$
language plpgsql;

我收到此错误column "_from" does not exist。如何解决这个问题?

【问题讨论】:

    标签: postgresql crosstab


    【解决方案1】:

    因为交叉表的参数只是字符串,所以必须直接包含参数:

    $$
    select  follow_up_by,next_follow_up_date,count(*) from sales_enquiry_follow_up where next_follow_up_date between $$ || quote_literal(_from) || $$ and $$ || quote_literal(_to) || $$
    group by follow_up_by,next_follow_up_date 
    order by follow_up_by,next_follow_up_date$$
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-11
      • 1970-01-01
      • 1970-01-01
      • 2019-07-25
      • 1970-01-01
      • 2015-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多