我正在使用Jaspersoft工作室创建报告。数据适配器是 postgresql 查询(到postgres服务器)。查询:
select * from dbo.FACTSALES where FactSales.saledatekey BETWEEN to_char($P!{StartDate}, 'YYYYMMDD')::INTEGER AND to_char($P!{EndDate}, 'YYYYMMDD')::INTEGER
FactSales.saledatekey
是Integer
。
我的计划是在报告中放入两个数据时间参数(StartDate,EndDate),以便我可以选择开始日期和结束日期来运行报告。所以查询如上所述,我还在Outline中创建了两个具有相同名称和类型java.sql.Timestamp(无默认表达式)的参数。但是当我运行报告时,我得到错误执行SQL语句。
修改
所以我换了$ P!到$ P
select * from dbo.FACTSALES where FactSales.saledatekey BETWEEN to_char($P{StartDate}, 'YYYYMMDD')::INTEGER AND to_char($P{EndDate}, 'YYYYMMDD')::INTEGER
但我仍然得到同样的错误。
jrxml (我从select *改为只有两列):
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="test4" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="fd68b751-49c2-4153-b2a8-48a95af021c9">
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="datamart"/>
<property name="ireport.jasperserver.url" value="http://10.20.169.43:8080/jasperserver/"/>
<property name="ireport.jasperserver.user" value="Nian"/>
<property name="ireport.jasperserver.report.resource" value="/reports/NianTest/test4_files/main_jrxml"/>
<property name="ireport.jasperserver.reportUnit" value="/reports/NianTest/test4"/>
<parameter name="StartDate" class="java.sql.Timestamp">
<parameterDescription><![CDATA[]]></parameterDescription>
</parameter>
<parameter name="EndDate" class="java.sql.Timestamp"/>
<queryString>
<![CDATA[select customerkey, productkey from dbo.FACTSALES where $X{[BETWEEN], FactSales.saledatekey , to_char($P{StartDate} , 'YYYYMMDD')::INTEGER, to_char($P{EndDate}, 'YYYYMMDD')::INTEGER}]]>
</queryString>
<field name="customerkey" class="java.lang.Integer"/>
<field name="productkey" class="java.lang.Integer"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch">
<staticText>
<reportElement x="50" y="0" width="100" height="30" uuid="1471918d-b246-4aea-ba5c-d65a7fa48284"/>
<text><![CDATA[customerkey]]></text>
</staticText>
<staticText>
<reportElement x="268" y="0" width="100" height="30" uuid="361e8408-7859-483f-8143-5834fff3594d"/>
<text><![CDATA[productkey]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<textField>
<reportElement x="50" y="49" width="100" height="30" uuid="3423ea0c-aa51-4537-ab28-29ae255c313c"/>
<textFieldExpression><![CDATA[$F{customerkey}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="200" y="40" width="100" height="30" uuid="d8381278-1fc1-4f9f-bd85-e65a85b77630"/>
<textFieldExpression><![CDATA[$F{productkey}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
和两个参数:
StartDate class: java.sql.Timestamp
EndDate class: java.sql.Timestamp
错误:
错误:函数to_char(未知,未知)不唯一提示:无法选择最佳候选函数。您可能需要添加显式类型转换
答案 0 :(得分:1)
使用预准备语句的查询将是:
select customerkey, productkey from dbo.FACTSALES where FactSales.saledatekey BETWEEN to_char($P{StartDate}::timestamp, 'YYYYMMDD')::int AND to_char($P{EndDate}::timestamp, 'YYYYMMDD')::int
或将参数更改为Integer
并在功能之间使用内置
select customerkey, productkey from dbo.FACTSALES where $X{[BETWEEN], FactSales.saledatekey ,StartDate, EndDate}
如果您想在IDE中测试,请始终考虑设置默认值参数。
<parameter name="StartDate" class="java.sql.Timestamp" isForPrompting="false">
<defaultValueExpression><![CDATA[new java.sql.Timestamp(1l)]]></defaultValueExpression>
</parameter>
<parameter name="EndDate" class="java.sql.Timestamp" isForPrompting="false">
<defaultValueExpression><![CDATA[new java.sql.Timestamp(new java.util.Date().getTime())]]></defaultValueExpression>
</parameter>