JasperReports Server:使用输入参数在报表中进行多选查询

时间:2014-05-22 09:51:31

标签: jasper-reports jasperserver input-parameters

请帮我在报告中传递输入参数,以便我可以使用参数的多选查询。

在我的报告中,我正在使用$x{IN,ADMIN1,admins}但正在使用

  

错误:参数admins的java.lang.string类型错误,admins应该是一个数组或集合。

Mysql查询:

Select ADMIN1,WEEK1,WEEK2,WEEK3,(((WEEK3-WEEK2)/WEEK2)*100) as percentage_change, (WEEK3-

WEEK2) as MSU_Difference,SUMWEEK2,SUMWEEK3,SUMWEEK,WEEK4,WEEK5,(((WEEK5-WEEK4)/WEEK4)*100)

 as percentage_change_PRE,admin,ORGNo,OrgAbbr,msus from ((select admin as ADMIN1, sum(msu) 

as WEEK1 from sccp_raw where dt >= date_sub(date($P{start_date}), INTERVAL 14 DAY) and dt 

< date_sub(date($P{end_date}), INTERVAL 13 DAY) group by admin order by WEEK1) as 

q1,(select admin as ADMIN2, sum(msu) as WEEK2 from sccp_raw where dt >= 

date_sub(date($P{start_date}), INTERVAL 7 DAY) and dt < date_sub(date($P{end_date}), 

INTERVAL 6 DAY) group by admin order by WEEK2) as q2,(select admin as ADMIN3, sum(msu) as 

WEEK3 from sccp_raw where dt between $P{start_date} and $P{end_date} group by admin order 

by WEEK3) as q3,(select sum(msu) as SUMWEEK2 from sccp_raw where dt >= 

date_sub(date($P{start_date}), INTERVAL 7 DAY) and dt < date_sub(date($P{end_date}), 

INTERVAL 6 DAY))as q4,(select sum(msu) as SUMWEEK3 from sccp_raw where dt >= 

date_sub(date($P{start_date}), INTERVAL 14 DAY) and dt < date_sub(date($P{end_date}), 

INTERVAL 13 DAY))as q5,(select sum(msu) as SUMWEEK from sccp_raw where dt BETWEEN 

$P{start_date} and $P{end_date})as q6,(select sum(msu) as WEEK4 from sccp_raw where dt >= 

date_sub(date($P{start_date}), INTERVAL 372 DAY) and dt < date_sub(date($P{end_date}), 

INTERVAL 371 DAY)) as q7,(select sum(msu) as WEEK5 from sccp_raw where dt >= 

date_sub(date($P{start_date}), INTERVAL 365 DAY) and dt < date_sub(date($P{end_date}), 

INTERVAL 364 DAY)) as q8,(select sum(msu) as MSU from sccp_raw where dt between 

$P{start_date} and $P{end_date}) as q9,(select a.dt,a.admin,b.ORGNo, b.OrgAbbr, sum(a.msu) 

as msus from sccp_raw a left join (select ORGNo, ADMINS, OrgAbbr from orig) b on 

a.admin=b.ADMINS where a.dt between $P{start_date} and $P{end_date} group by a.admin)as 

q10)  where $X{IN,ADMIN1,admins} and ADMIN1=ADMIN3 and ADMIN2=ADMIN3 and admin=ADMIN3 

group by ADMIN1 order by WEEK3 desc; 

参数的配置:

parameter "admins":  

class: `java.util.Collection`  

Default Value Expression: "select admin from sccp_raw group by admin"  

还帮助我知道我是否要创建此下拉菜单以选择为&#34;所有管理员&#34;其次是管理员列表。

2 个答案:

答案 0 :(得分:0)

对于Collection类型的multiselect参数,你不需要给“All”选择iReport中的所有值作为该参数的默认值,默认情况下意味着选择了所有值。

但是如果参数是单选,那么jasperreport服务器输入控件的查询将是输入控件值中的“All”: -

 SELECT * FROM (
 SELECT 'All Admins' admin from sccp_raw 
 UNION
 SELECT DISTINCT admin from sccp_raw) a
 ORDER BY admin

和iReport查询将是: -

  SELECT *
  FROM table
  WHERE ($P{admins}='All Admins' OR Admin=$P{admins}) 

例如,在ireport中使用示例HSQL数据库查看此报告: -

  <?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="report1"     
  pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20"  
   topMargin="20" bottomMargin="20" uuid="0d2f9917-101e-4d51-9d78-172909ca097a"> 
<property name="ireport.zoom" value="1.0"/> 
<property name="ireport.x" value="0"/> 
<property name="ireport.y" value="0"/> 
<parameter name="p_SHIPCOUNTRY" class="java.lang.String">
    <defaultValueExpression><![CDATA["Germany"]]></defaultValueExpression>
</parameter>
<queryString>
    <![CDATA[SELECT SHIPCOUNTRY,SHIPCITY

    FROM orders
    WHERE  ($P{p_SHIPCOUNTRY}='All' OR SHIPCOUNTRY=$P{p_SHIPCOUNTRY})]]>
</queryString>
<field name="SHIPCOUNTRY" class="java.lang.String"/>
<field name="SHIPCITY" class="java.lang.String"/>
<background>
    <band splitType="Stretch"/>
</background>
<detail>
    <band height="29">
        <textField>
            <reportElement uuid="f6cbe144-e64a-4537-b3b8-e6a461dd5a72"
                   x="114" y="0" width="100" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{SHIPCOUNTRY}]]>
                </textFieldExpression>
        </textField>
        <textField>

            <reportElement uuid="c13e2483-d005-4f56-8ce8-d64f923f0359"
                 x="417" y="3" width="100" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{SHIPCITY}]]>             
        </textFieldExpression>
        </textField>
    </band>
        </detail>

            </jasperReport>

对于mor详细信息,您可以访问我的blog

答案 1 :(得分:0)

您必须在jrxm文件中使用java.util.Collection。

Here, send your multi ids from your selection to your .jasper file.
      String qGelirGrubu = "1,2,3,4,5";
      String[] convertedGelirArray = qGelirGrubu.split(",");
      List<Integer> convertedGelirList = new ArrayList<Integer>();
      for (String number : convertedGelirArray) {
           convertedGelirList.add(Integer.parseInt(number.trim()));
      }
     parameters.put("qGelirGrubu1", convertedGelirList);

在您的.jrxml文件中; 像这样定义您的参数;

<parameter name="qGelirGrubu1" class="java.util.Collection"/>

和您的SQL查询,您可以像这样使用参数;

从TABLE_NAME A中选择*,其中A.ID IN(1,2,3,4,5);就像.jrxml文件中的那样:

 select * from TABLE_NAME A where  $X{IN,A.ID,qGelirGrubu1} 

$ X {IN,COLUMN_NAME,YOUR_PARAMETER_NAME}