Java 在JasperReports(iReport)中对数据集上的连续数字进行分组

Java 在JasperReports(iReport)中对数据集上的连续数字进行分组,java,sql,jasper-reports,ireport,Java,Sql,Jasper Reports,Ireport,我在jasper报告中有一个查询,其中一个由用户给定输入生成的结果集可能是以下数据: memberId stockNo refineryCode constantSerialNo serialNo 45 1 IAR A- 98729 45 1 IAR A- 98730 45 1 IAR A-

我在jasper报告中有一个查询,其中一个由用户给定输入生成的结果集可能是以下数据:

memberId stockNo refineryCode constantSerialNo  serialNo
45       1       IAR          A-                98729
45       1       IAR          A-                98730
45       1       IAR          A-                98731
45       1       IAR          A-                98733
45       1       IAR          A-                98734
45       1       IAR          A-                98736
45       1       IAR          A-                98737
45       1       IAR          A-                98738
45       1       IAR          A-                98739
如果序列号是连续的,我想在一行上显示这些数据。因此,如果我想在报告中显示上述数据,我必须以以下方式显示它们:

memberId stockNo refineryCode constantSerialNo  serialNo
45       1       IAR          A-                98729 - 98731
45       1       IAR          A-                98733 - 98734
45       1       IAR          A-                98736 - 98739

我知道可能会有一些解决方案在sql上使用游标,或者在像Java这样的OOP语言中使用ORM并发送到jasper报告。然而,出于好奇,我想问一下,是否有一种使用iReport的表达式或组的动态解决方案,或者其他我现在没有想到的东西,可以让我的生活更轻松。

这里有一种方法,假设
serialNo
是某种数值而不是字符串

  • sql查询结果的数据集必须按
    serialNo
    排序,如上所示
  • 使用以下组表达式创建一个报告组:
    $V{GroupCount}==new BigDecimal(0)$F{serialNo}:$F{serialNo}.subtract($V{GroupCount})
    。这将在
    serialNo
    序列中每次出现间隙时创建一个新组
  • 创建一个变量
    GroupCount
    ,用于计算当前组中连续
    serialNo
    值的数量。此变量用于跟踪下一个预期的
    serialNo
  • 创建一个变量
    StartRange
    ,该变量将保存给定范围的连续
    serialNo
    值的起始值。该值应在新组开始时重置,并具有以下值:
    $V{GroupCount}.intValue()==1$F{ID}:$V{StartRange}
  • serialNo
    字段的
    Evaluation Time
    值设置为
    ReportGroup
    。将以下值放入其中:
    $V{StartRange}+“-”+$F{serialNo}
  • Detail
    带的
    Print-When表达式更改为以下内容:
    new Boolean($V{GroupCount}.intValue()==1)
  • 我在下面发布了完整的jrxml代码

    <?xml version="1.0" encoding="utf-8"?>
    <!-- Created with iReport - A designer for JasperReports -->
    <!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"
    "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
    <jasperReport name="Group_By_Consecutive" columnCount="1" printOrder="Vertical" orientation="Portrait" pageWidth="595" pageHeight="842" columnWidth="535" columnSpacing="0" leftMargin="30" rightMargin="30" topMargin="20" bottomMargin="20" whenNoDataType="NoPages" isTitleNewPage="false" isSummaryNewPage="false">
      <property name="ireport.scriptlethandling" value="0" />
      <property name="ireport.encoding" value="UTF-8" />
      <import value="java.util.*" />
      <import value="net.sf.jasperreports.engine.*" />
      <import value="net.sf.jasperreports.engine.data.*" />
      <queryString>
        <![CDATA[select 1 as id, 'Name 1' as name from dual union all
    select 2 as id, 'Name 2' as name from dual union all
    select 3 as id, 'Name 3' as name from dual union all
    select 4 as id, 'Name 4' as name from dual union all
    select 6 as id, 'Name 6' as name from dual union all
    select 7 as id, 'Name 7' as name from dual union all
    select 8 as id, 'Name 8' as name from dual union all
    select 9 as id, 'Name 9' as name from dual union all
    select 10 as id, 'Name 10' as name from dual union all
    select 11 as id, 'Name 11' as name from dual union all
    select 14 as id, 'Name 14' as name from dual union all
    select 15 as id, 'Name 15' as name from dual union all
    select 16 as id, 'Name 16' as name from dual union all
    select 17 as id, 'Name 17' as name from dual union all
    select 23 as id, 'Name 23' as name from dual union all
    select 24 as id, 'Name 24' as name from dual union all
    select 25 as id, 'Name 25' as name from dual union all
    select 26 as id, 'Name 26' as name from dual union all
    select 27 as id, 'Name 27' as name from dual union all
    select 28 as id, 'Name 28' as name from dual]]>
    </queryString>
      <field name="ID" class="java.math.BigDecimal" />
      <field name="NAME" class="java.lang.String" />
      <variable name="GroupCount" class="java.math.BigDecimal" resetType="Group" resetGroup="Consecutive" calculation="Count">
        <variableExpression>
          <![CDATA[$F{ID}]]>
    </variableExpression>
        <initialValueExpression>
          <![CDATA[new BigDecimal(0)]]>
    </initialValueExpression>
      </variable>
      <variable name="StartRange" class="java.math.BigDecimal" resetType="Report" calculation="Nothing">
        <variableExpression>
          <![CDATA[$V{GroupCount}.intValue() == 1 ? $F{ID}:$V{StartRange}]]>
    </variableExpression>
      </variable>
      <group name="Consecutive">
        <groupExpression>
          <![CDATA[$V{GroupCount} == new BigDecimal(0) ? $F{ID}:$F{ID}.subtract($V{GroupCount})]]>
    </groupExpression>
        <groupHeader>
          <band height="0" isSplitAllowed="true"></band>
        </groupHeader>
        <groupFooter>
          <band height="0" isSplitAllowed="true"></band>
        </groupFooter>
      </group>
      <background>
        <band height="0" isSplitAllowed="true"></band>
      </background>
      <title>
        <band height="0" isSplitAllowed="true"></band>
      </title>
      <pageHeader>
        <band height="0" isSplitAllowed="true"></band>
      </pageHeader>
      <columnHeader>
        <band height="18" isSplitAllowed="true">
          <staticText>
            <reportElement x="0" y="0" width="200" height="18" key="staticText-1" />
            <box></box>
            <textElement>
              <font />
            </textElement>
            <text>
              <![CDATA[serialNo]]>
    </text>
          </staticText>
          <staticText>
            <reportElement x="200" y="0" width="100" height="18" key="staticText-3" />
            <box></box>
            <textElement>
              <font />
            </textElement>
            <text>
              <![CDATA[Name]]>
    </text>
          </staticText>
          <staticText>
            <reportElement x="300" y="0" width="100" height="18" key="staticText-4" />
            <box></box>
            <textElement>
              <font />
            </textElement>
            <text>
              <![CDATA[MetaData(Ignore)]]>
    </text>
          </staticText>
        </band>
      </columnHeader>
      <detail>
        <band height="18" isSplitAllowed="true">
          <printWhenExpression>
            <![CDATA[new Boolean($V{GroupCount}.intValue() == 1)]]>
    </printWhenExpression>
          <textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self">
            <reportElement x="200" y="0" width="100" height="18" key="textField" />
            <box></box>
            <textElement>
              <font />
            </textElement>
            <textFieldExpression class="java.lang.String">
              <![CDATA[$F{NAME}]]>
    </textFieldExpression>
          </textField>
          <textField isStretchWithOverflow="false" pattern="##0.00" isBlankWhenNull="false" evaluationTime="Group" evaluationGroup="Consecutive" hyperlinkType="None" hyperlinkTarget="Self">
            <reportElement x="0" y="0" width="200" height="18" key="textField" />
            <box></box>
            <textElement>
              <font />
            </textElement>
            <textFieldExpression class="java.lang.String">
              <![CDATA[$V{StartRange}+" - " + $F{ID}]]>
    </textFieldExpression>
          </textField>
          <textField isStretchWithOverflow="false" pattern="##0.00" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self">
            <reportElement x="300" y="0" width="100" height="18" key="textField" />
            <box></box>
            <textElement>
              <font />
            </textElement>
            <textFieldExpression class="java.math.BigDecimal">
              <![CDATA[$V{GroupCount}]]>
    </textFieldExpression>
          </textField>
        </band>
      </detail>
      <columnFooter>
        <band height="0" isSplitAllowed="true"></band>
      </columnFooter>
      <pageFooter>
        <band height="0" isSplitAllowed="true"></band>
      </pageFooter>
      <summary>
        <band height="0" isSplitAllowed="true"></band>
      </summary>
    </jasperReport>
    
    
    
    在orcale中,有一种方法可以创建一个查询,该查询会给出这样的结果。除了序列号是987299873098731你是对的,必须有一种方法可以通过查询实现我想要的功能,但我想要一种解决方案,即使用jasper reports-iReport。您可以在iReport中使用数据透视表来实现。您的意思是编写查询并将其放在iReport中的报告查询部分??您可以在Oracle SQL中获得所需的功能。这能解决你的问题吗?还是你想在报道方面做到这一点?这对你来说是非常令人印象深刻的,也是富有创造性的。唯一的事情是将第一个Textfield列更改为
    $V{StartRange}==$F{ID}$F{ID}.toString():$V{StartRange}+“-”+$F{ID}
    ,因为当组中只有一个项目时,它不会打印破折号。要了解我的意思,请取出select for id 10。这将使您自己在一个组中有11项。另外,由于您只打印组中的第一项,所以GroupCount列始终有1项,但我认为在这种情况下这并不重要,因为我们并不真正关心组中有多少项。@jschoen nice catch,我从未考虑过这种情况。@user845279非常感谢您。。。你的解决方案太棒了。这对我的案子有效。