简体   繁体   English

我可以在Pentaho Report Designer中的查询中使用用户定义的数据库功能吗?

[英]Can I use a user defined database function in a query in Pentaho Report Designer?

I'm reporting on data from two tables that don't have a sane way to join together. 我要报告的是来自两个表的数据,这些表没有一种健全的方式将它们连接在一起。 Basically it's inventory in one table, sales in the other, and I'm trying to get the days of inventory on hand by dividing the two. 基本上是一张表中的库存,另一张表中的销售,我试图通过将两者相除来获得现有的库存天数。 Since I couldn't think of a way to join the tables I abstracted one query into a database function and called it from the other. 由于我想不出连接表的方法,因此将一个查询抽象到数据库函数中,然后从另一个查询中调用它。

Here is the function definition: 这是函数定义:

CREATE OR REPLACE FUNCTION avgsales(date, text, text, integer) RETURNS numeric
    AS ' SELECT sum(quantity)/(65.0*$4/90.0) as thirty_day_avg
         FROM data_867 JOIN drug_info 
         ON drug_info.dist_ndc = trim(leading ''0'' from data_867.product_ndc)
         WHERE
         rpt_start_dt>= $1-$4 AND
         rpt_end_dt<= $1 AND
         drug_info.drug_name = $2 AND
         wholesaler_name = $3 '
    LANGUAGE SQL;

And here is the report query: 这是报告查询:

SELECT
(sum("data_852"."za02")/5)/avgsales(date '2010-11-30', 'Semprex D 100ct', 'McKesson', 30) as doh
FROM
"data_852"
JOIN
"drug_info" ON "drug_info"."dist_ndc" = "data_852"."lin03"
JOIN
"wholesaler_info" ON trim("data_852"."isa06") = trim("wholesaler_info"."isa06")
WHERE
(za01 = 'QA'
OR za01 = 'QP'
OR za01 = 'QI')
and "data_852"."xq02">= DATE '2010-11-30'-5
and "data_852"."xq03"<='2010-11-30'
and drug_info.drug_name = 'Semprex D 100ct'
and wholesaler_info.wholesaler_name = 'McKesson'
;

As it is here, it will run in Pentaho report designer but this is hard coded. 如此处所示,它将在Pentaho报表设计器中运行,但这是硬编码的。 When I parameterize the values for the where clause it complains about a syntax error at $1. 当我对where子句的值进行参数化时,它抱怨$ 1出现语法错误。 From looking at the queries that Postgres receives, Pentaho passes the query with it's parameters using $1, $2, etc. I think there might be a conflict with the same variable names being used in our function, or maybe it's just a data type problem. 通过查看Postgres接收到的查询,Pentaho使用$ 1,$ 2等传递带有其参数的查询。我认为我们的函数中使用的相同变量名可能存在冲突,或者可能只是数据类型问题。

What could be causing this error? 是什么导致此错误? Is it possible to use a function like this in the report query? 是否可以在报表查询中使用类似这样的功能? If not, how can I do something similar using the Report Designer? 如果没有,如何使用报表设计器执行类似的操作?

It is possible. 有可能的。 I am using Postgres 8.4 and RD 3.7 我正在使用Postgres 8.4和RD 3.7

create function ret_p(text) 
returns text 
as 
$$ 
select $1;
$$ language sql immutable;

Report designer query 报表设计器查询

select * from ret_p(${p_val});

where p_val is the parameter name as defined in RD 其中p_val是RD中定义的参数名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM