2012-07-16 13 views
5

Próbuję utworzyć tabelę podsumowań za pomocą iReport. Mój zestaw danych zwraca listę zakupów i cen. Coś jakJak sumować określone wiersze w iReporcie za pomocą zapytania?

Milk, $1.23

Chicken, $5.45

Milk, $1.44

i tak dalej. Chcę, żeby mój stół mógł rozbić mój przedmiot według produktu. Chcę tabelę z kolumnami:

  • # razy produkt został zakupiony (#rows gdzie produkt mleczny),
  • łączna kwota dolar wydany na produkcie (suma cen gdzie produkt jest mleko),
  • i średnia cena produktu (kolumna 2 podzielona przez kolumnę 1).

Jak mogę to zrobić? Grałem ze zmiennymi i mogę uzyskać całkowitą sumę wszystkich cen, ale nie wiem jak to zrobić z podzestawem danych przy użyciu bardziej złożonego zapytania.

Odpowiedz

8

To całkiem proste. Możesz utworzyć grupę na polu produkt i dwie zmienne w tej grupie: dla zliczania sumy wynikowej i sumy średniej. Za pomocą wbudowanej zmiennej można obliczyć i wyświetlić liczbę elementów w tej grupie.

Próbka ta działa:

<?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="group_average2" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"> 
    <queryString> 
     <![CDATA[]]> 
    </queryString> 
    <field name="product" class="java.lang.String"/> 
    <field name="price" class="java.lang.Integer"/> 
    <sortField name="product"/> 
    <variable name="totalSum" class="java.lang.Double" resetType="Group" resetGroup="productGroup" calculation="Sum"> 
     <variableExpression><![CDATA[$F{price}]]></variableExpression> 
     <initialValueExpression><![CDATA[0]]></initialValueExpression> 
    </variable> 
    <variable name="averageSum" class="java.lang.Double" resetType="Group" resetGroup="productGroup" calculation="Average"> 
     <variableExpression><![CDATA[$F{price}]]></variableExpression> 
     <initialValueExpression><![CDATA[0]]></initialValueExpression> 
    </variable> 
    <group name="productGroup"> 
     <groupExpression><![CDATA[$F{product}]]></groupExpression> 
     <groupFooter> 
      <band height="20"> 
       <textField> 
        <reportElement x="0" y="0" width="122" height="20"/> 
        <box leftPadding="10" rightPadding="10"> 
         <topPen lineWidth="1.0"/> 
         <leftPen lineWidth="1.0"/> 
         <bottomPen lineWidth="1.0"/> 
         <rightPen lineWidth="1.0"/> 
        </box> 
        <textElement/> 
        <textFieldExpression><![CDATA[$F{product}]]></textFieldExpression> 
       </textField> 
       <textField pattern="###0.00;-###0.00"> 
        <reportElement x="244" y="0" width="122" height="20"/> 
        <box leftPadding="10" rightPadding="10"> 
         <topPen lineWidth="1.0"/> 
         <leftPen lineWidth="1.0"/> 
         <bottomPen lineWidth="1.0"/> 
         <rightPen lineWidth="1.0"/> 
        </box> 
        <textElement textAlignment="Right"/> 
        <textFieldExpression><![CDATA[$V{totalSum}]]></textFieldExpression> 
       </textField> 
       <textField> 
        <reportElement x="122" y="0" width="122" height="20"/> 
        <box leftPadding="10" rightPadding="10"> 
         <topPen lineWidth="1.0"/> 
         <leftPen lineWidth="1.0"/> 
         <bottomPen lineWidth="1.0"/> 
         <rightPen lineWidth="1.0"/> 
        </box> 
        <textElement/> 
        <textFieldExpression><![CDATA[$V{productGroup_COUNT}]]></textFieldExpression> 
       </textField> 
       <textField pattern="###0.00;-###0.00"> 
        <reportElement x="366" y="0" width="149" height="20"/> 
        <box leftPadding="10" rightPadding="10"> 
         <topPen lineWidth="1.0"/> 
         <leftPen lineWidth="1.0"/> 
         <bottomPen lineWidth="1.0"/> 
         <rightPen lineWidth="1.0"/> 
        </box> 
        <textElement textAlignment="Right"/> 
        <textFieldExpression><![CDATA[$V{averageSum}]]></textFieldExpression> 
       </textField> 
      </band> 
     </groupFooter> 
    </group> 
    <pageHeader> 
     <band height="19"> 
      <staticText> 
       <reportElement x="0" y="-1" width="122" height="20"/> 
       <box leftPadding="10" rightPadding="10"> 
        <topPen lineWidth="1.0"/> 
        <leftPen lineWidth="1.0"/> 
        <bottomPen lineWidth="1.0"/> 
        <rightPen lineWidth="1.0"/> 
       </box> 
       <textElement textAlignment="Center" verticalAlignment="Middle" markup="none"> 
        <font isBold="true" isItalic="true"/> 
       </textElement> 
       <text><![CDATA[Product]]></text> 
      </staticText> 
      <staticText> 
       <reportElement x="122" y="-1" width="122" height="20"/> 
       <box leftPadding="10" rightPadding="10"> 
        <topPen lineWidth="1.0"/> 
        <leftPen lineWidth="1.0"/> 
        <bottomPen lineWidth="1.0"/> 
        <rightPen lineWidth="1.0"/> 
       </box> 
       <textElement textAlignment="Center" verticalAlignment="Middle" markup="none"> 
        <font isBold="true" isItalic="true"/> 
       </textElement> 
       <text><![CDATA[Number of orders]]></text> 
      </staticText> 
      <staticText> 
       <reportElement x="244" y="-1" width="122" height="20"/> 
       <box leftPadding="10" rightPadding="10"> 
        <topPen lineWidth="1.0"/> 
        <leftPen lineWidth="1.0"/> 
        <bottomPen lineWidth="1.0"/> 
        <rightPen lineWidth="1.0"/> 
       </box> 
       <textElement textAlignment="Center" verticalAlignment="Middle" markup="none"> 
        <font isBold="true" isItalic="true"/> 
       </textElement> 
       <text><![CDATA[Total sum]]></text> 
      </staticText> 
      <staticText> 
       <reportElement x="366" y="-1" width="149" height="20"/> 
       <box leftPadding="10" rightPadding="10"> 
        <topPen lineWidth="1.0"/> 
        <leftPen lineWidth="1.0"/> 
        <bottomPen lineWidth="1.0"/> 
        <rightPen lineWidth="1.0"/> 
       </box> 
       <textElement textAlignment="Center" verticalAlignment="Middle" markup="none"> 
        <font isBold="true" isItalic="true"/> 
       </textElement> 
       <text><![CDATA[Average sum of order]]></text> 
      </staticText> 
     </band> 
    </pageHeader> 
</jasperReport> 

Próbka ta zajmuje plik CSV jako źródła danych. Możesz edytować zapytanie - do pobierania danych za pomocą zapytania SQL.

projektu raportu w iReport jest:

The report design

Rezultatem będzie (poprzez podgląd w iReport):

The result in iReport

należy przeczytać ten artykuł o using variables in JasperReports i ten post o using groups in JasperReports.

Proszę zapamiętaj, że należy posortować dane przed użyciem grupy. Cytat z postu o grupowania i sortowania z jasperforge.org:

In order to get an accurate data representation, the data in the data source should be already ordered according to the group expressions used in the report. One can either perform data sorting through the report query, or use the <sortField/> element.

+0

Dziękujemy! Sztuczka, której mi brakowało, to zespół grupy. Część o IReport, z którą walczę, to zespoły. Ta sama zmienna będzie wyświetlać różne wyniki w zależności od pasma, w którym się znajduje. Dlaczego ma znaczenie, czy dane są sortowane czy nie? – Marianna

+0

Jako dodatek do mojego pierwotnego problemu ... Czy istnieje sposób użycia mnożnika do mojej całkowitej kwoty? Mam dodatkową kolumnę, stan, wskazując stan, w którym został zakupiony. Mam tabelę STATE_SALES_TAX, które chcę kwerendy dla każdego rekordu. Jeśli kupisz moje mleko w NJ, podatek od sprzedaży wynosi 1,06. Kupuję kolejny galon w NY i jest to 1,08.Czy istnieje sposób na zsumowanie sumy wydanej na mleko za pomocą tego mnożnika z innej tabeli? Całkowite wydatki na mleko = suma (każdyMilkPx * salesTax (statePurchased)) – Marianna

+0

Tak, możesz utworzyć wyrażenie dowolnej zmiennej, którego potrzebujesz. –

Powiązane problemy