用于Crystal Reports SQL命令的Postgresql参数化查询

问题描述

我在Crystal报表中将以下Postgresql select语句用作sql命令,它的运行效果很好。现在,我想对其进行更改,以便它将传递给它的日期用作参数,而不是像下面这样对它们进行硬编码(参数是year,monthstart和monthend)。

我用于运行报告的程序(Spire)在运行报告时会传递诸如?ReportStart或?ReportEnd之类的参数。我尝试用?ReportStart替换monthstart日期“ 2020-04-01”(例如:?ReportStart :: date作为monthstart),但出现错误,指出“列“ reportstart”不存在”。

我还尝试了在Postgresql PREPARE文档中阅读的1 $,2 $和3 $,但似乎我不太了解如何正确编写。

``` with params as (select '2019-01-01'::date as year,'2020-04-01'::date as monthstart,'2020-04-30'::date as monthend) SELECT h.salesperson_no,h.territory_code,h.cust_no,h.cust_name,i.part_no,i.description,SUM(i.committed_qty) FILTER
    (WHERE h.invoice_date >= p.year AND h.invoice_date < p.year + interval '1 year') as LYQty,SUM(i.committed_qty * i.unit_price) FILTER
    (WHERE h.invoice_date >= p.year AND h.invoice_date < p.year + interval '1 year') as LYSales,SUM(i.committed_qty * i.unit_price) FILTER
    (WHERE h.invoice_date >= p.year AND h.invoice_date < p.monthend - interval '1 year') as LYTDSales,SUM(i.committed_qty) FILTER
    (WHERE h.invoice_date >= p.year + interval '1 year' AND h.invoice_date < p.year + interval '2 years') as YTDQty,SUM(i.committed_qty * i.unit_price) FILTER
    (WHERE h.invoice_date >= p.year + interval '1 year' AND h.invoice_date < p.year + interval '2 years') as YTDSales,SUM(i.committed_qty) FILTER 
     (WHERE h.invoice_date >= p.monthstart AND h.invoice_date <= p.monthend) as MTDQty,SUM(i.committed_qty * i.unit_price) FILTER
     (WHERE h.invoice_date >= p.monthstart AND h.invoice_date <= p.monthend) as MTDSales FROM params p
   CROSS JOIN sales_history h
   LEFT JOIN sales_history_items i
          ON i.invoice_no = h.invoice_no WHERE i.part_no is not null AND h.invoice_date >= p.year AND h.invoice_date < p.year + interval '2 years' GROUP BY h.salesperson_no,i.description ```

对于将参数正确添加到postgresql中的select语句的任何指导,将不胜感激。

P.S。抱歉,您必须在此代码段上向右滚动。由于某些原因,如果不删除一些硬性报酬,我就无法将代码全部保留在一个框中。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)