月中天数的变量列

问题描述 投票:0回答:0

想象一下,我有一份报告显示每月的每一天的销售额,按国家/地区分组(这很重要)。解决这个问题的最佳方法是什么?

大家好!我是 Jasper Reports 的新手,需要一些帮助。 为了解决我的问题,我创建了这个例子。 想象一下,我有一份报告显示每月的每一天的销售额,按国家/地区分组(这很重要)。 在这一点上,我已经尝试使用交叉表并得到了this result

这里有2个问题:

  1. 数据集没有关系,两国的交叉表显示相同的结果。也许我可以将国家/地区作为参数传递给 subDataSet?

  2. 报表中没有days在items_sales表中没有对应的记录。 我可以生成数天数为空的记录,这些记录不在数据集中。我没意见,如果报告能正常工作的话。

  3. 布局问题。组标题和交叉表中的国家/地区名称单元格具有不同的宽度。

我还尝试了另一种方法:为每个月的每一天添加字段和列,并根据特定月份隐藏 31、30、29。但是我得到了layout problems。这是具有固定布局的报告文件:forks-and-spoons-fixed.jrxml.

如何解决这样的问题?解决这个问题的最佳方法是什么?

forks-and-spoons.jrxml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.20.0.final using JasperReports Library version 6.20.0-2bc7ab61c56f459e8176eb05c7705e145cd400ad  -->
<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="forks-and-spoons" pageWidth="1191" pageHeight="842" orientation="Landscape" columnWidth="1151" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="e56c1564-4137-482a-bd33-dbdae76075ae">
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="localTst"/>
    <property name="com.jaspersoft.studio.unit." value="pixel"/>
    <property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.bottomMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.leftMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/>
    <style name="Crosstab_CH" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Crosstab_CG" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Crosstab_CT" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Crosstab_CD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="subDataset" uuid="027fc84b-6a65-4218-9bce-08bb389def44">
        <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="localTst"/>
        <queryString language="SQL">
            <![CDATA[select i.item_id,i.name, s.country, s.day, s.amount
from items i
join items_sales s on i.item_id = s.item_id]]>
        </queryString>
        <field name="item_id" class="java.lang.Integer">
            <property name="com.jaspersoft.studio.field.name" value="item_id"/>
            <property name="com.jaspersoft.studio.field.label" value="item_id"/>
            <property name="com.jaspersoft.studio.field.tree.path" value="items"/>
        </field>
        <field name="name" class="java.lang.String">
            <property name="com.jaspersoft.studio.field.name" value="name"/>
            <property name="com.jaspersoft.studio.field.label" value="name"/>
            <property name="com.jaspersoft.studio.field.tree.path" value="items"/>
        </field>
        <field name="country" class="java.lang.String">
            <property name="com.jaspersoft.studio.field.name" value="country"/>
            <property name="com.jaspersoft.studio.field.label" value="country"/>
            <property name="com.jaspersoft.studio.field.tree.path" value="items_sales"/>
        </field>
        <field name="day" class="java.lang.Integer">
            <property name="com.jaspersoft.studio.field.name" value="day"/>
            <property name="com.jaspersoft.studio.field.label" value="day"/>
            <property name="com.jaspersoft.studio.field.tree.path" value="items_sales"/>
        </field>
        <field name="amount" class="java.lang.Integer">
            <property name="com.jaspersoft.studio.field.name" value="amount"/>
            <property name="com.jaspersoft.studio.field.label" value="amount"/>
            <property name="com.jaspersoft.studio.field.tree.path" value="items_sales"/>
        </field>
    </subDataset>
    <queryString language="SQL">
        <![CDATA[select i.item_id,i.name, s.country, s.day, s.amount
from items i
join items_sales s on i.item_id = s.item_id]]>
    </queryString>
    <field name="item_id" class="java.lang.Integer">
        <property name="com.jaspersoft.studio.field.name" value="item_id"/>
        <property name="com.jaspersoft.studio.field.label" value="item_id"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="items"/>
    </field>
    <field name="name" class="java.lang.String">
        <property name="com.jaspersoft.studio.field.name" value="name"/>
        <property name="com.jaspersoft.studio.field.label" value="name"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="items"/>
    </field>
    <field name="country" class="java.lang.String">
        <property name="com.jaspersoft.studio.field.name" value="country"/>
        <property name="com.jaspersoft.studio.field.label" value="country"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="items_sales"/>
    </field>
    <field name="day" class="java.lang.Integer">
        <property name="com.jaspersoft.studio.field.name" value="day"/>
        <property name="com.jaspersoft.studio.field.label" value="day"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="items_sales"/>
    </field>
    <field name="amount" class="java.lang.Integer">
        <property name="com.jaspersoft.studio.field.name" value="amount"/>
        <property name="com.jaspersoft.studio.field.label" value="amount"/>
        <property name="com.jaspersoft.studio.field.tree.path" value="items_sales"/>
    </field>
    <sortField name="country"/>
    <group name="country">
        <groupExpression><![CDATA[$F{country}]]></groupExpression>
        <groupHeader>
            <band height="65" splitType="Stretch">
                <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.FreeLayout"/>
                <property name="com.jaspersoft.studio.unit.height" value="px"/>
                <textField textAdjust="StretchHeight" isBlankWhenNull="true">
                    <reportElement mode="Opaque" x="0" y="0" width="1151" height="25" backcolor="#FAEEED" uuid="c175dddf-e626-46c6-a19e-2356627bf1dc">
                        <property name="com.jaspersoft.studio.unit.height" value="px"/>
                    </reportElement>
                    <box>
                        <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                        <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                        <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                        <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    </box>
                    <textElement verticalAlignment="Middle"/>
                    <textFieldExpression><![CDATA[$F{country}]]></textFieldExpression>
                </textField>
                <crosstab>
                    <reportElement positionType="Float" stretchType="ElementGroupHeight" x="0" y="25" width="1151" height="40" uuid="e85787f2-03ab-40c1-a28c-ec211e1c248f">
                        <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.HorizontalRowLayout"/>
                        <property name="com.jaspersoft.studio.crosstab.style.header" value="Crosstab_CH"/>
                        <property name="com.jaspersoft.studio.crosstab.style.group" value="Crosstab_CG"/>
                        <property name="com.jaspersoft.studio.crosstab.style.total" value="Crosstab_CT"/>
                        <property name="com.jaspersoft.studio.crosstab.style.detail" value="Crosstab_CD"/>
                    </reportElement>
                    <crosstabDataset>
                        <dataset resetType="Report">
                            <datasetRun subDataset="subDataset" uuid="44c290d3-1003-43ce-9f9b-a1dd512e70a5">
                                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                            </datasetRun>
                        </dataset>
                    </crosstabDataset>
                    <crosstabHeaderCell>
                        <cellContents>
                            <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.HorizontalRowLayout"/>
                            <staticText>
                                <reportElement x="0" y="0" width="60" height="20" uuid="eb2a2f5e-c944-4950-ab7c-22562349fc8f"/>
                                <box>
                                    <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                </box>
                                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                                <text><![CDATA[Id]]></text>
                            </staticText>
                            <staticText>
                                <reportElement x="60" y="0" width="60" height="20" uuid="866cab22-42fb-44dc-910f-25737f77b5b8"/>
                                <box>
                                    <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                </box>
                                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                                <text><![CDATA[Name]]></text>
                            </staticText>
                        </cellContents>
                    </crosstabHeaderCell>
                    <rowGroup name="item_id" width="60">
                        <bucket class="java.lang.Integer">
                            <bucketExpression><![CDATA[$F{item_id}]]></bucketExpression>
                        </bucket>
                        <crosstabRowHeader>
                            <cellContents mode="Opaque" style="Crosstab_CH">
                                <textField>
                                    <reportElement x="0" y="0" width="60" height="20" uuid="e599cdec-d797-4291-8c04-2d04d8e2b169"/>
                                    <box>
                                        <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    </box>
                                    <textElement textAlignment="Center" verticalAlignment="Middle"/>
                                    <textFieldExpression><![CDATA[$V{item_id}]]></textFieldExpression>
                                </textField>
                            </cellContents>
                        </crosstabRowHeader>
                        <crosstabTotalRowHeader>
                            <cellContents mode="Opaque" style="Crosstab_CT">
                                <staticText>
                                    <reportElement x="0" y="0" width="120" height="20" uuid="bfc19290-7921-4c12-b83b-07f4885c2760"/>
                                    <text><![CDATA[Total item_id]]></text>
                                </staticText>
                            </cellContents>
                        </crosstabTotalRowHeader>
                    </rowGroup>
                    <rowGroup name="name" width="60">
                        <bucket class="java.lang.String">
                            <bucketExpression><![CDATA[$F{name}]]></bucketExpression>
                        </bucket>
                        <crosstabRowHeader>
                            <cellContents mode="Opaque" style="Crosstab_CH">
                                <textField>
                                    <reportElement x="0" y="0" width="60" height="20" uuid="8e9503a0-36f5-4f2c-bf1c-5f0febf18de4"/>
                                    <box>
                                        <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    </box>
                                    <textElement textAlignment="Center" verticalAlignment="Middle"/>
                                    <textFieldExpression><![CDATA[$V{name}]]></textFieldExpression>
                                </textField>
                            </cellContents>
                        </crosstabRowHeader>
                        <crosstabTotalRowHeader>
                            <cellContents mode="Opaque" style="Crosstab_CG">
                                <staticText>
                                    <reportElement x="0" y="0" width="60" height="20" uuid="887fed1a-fd21-4ddd-a94d-dd9b91457e49"/>
                                    <text><![CDATA[Total name]]></text>
                                </staticText>
                            </cellContents>
                        </crosstabTotalRowHeader>
                    </rowGroup>
                    <columnGroup name="day" height="20" totalPosition="Start">
                        <bucket class="java.lang.Integer">
                            <bucketExpression><![CDATA[$F{day}]]></bucketExpression>
                        </bucket>
                        <crosstabColumnHeader>
                            <cellContents mode="Opaque" style="Crosstab_CH">
                                <textField>
                                    <reportElement x="0" y="0" width="60" height="20" uuid="8c26a835-020d-4384-966d-1774bb4d2b28"/>
                                    <box>
                                        <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    </box>
                                    <textElement textAlignment="Center" verticalAlignment="Middle"/>
                                    <textFieldExpression><![CDATA[$V{day}]]></textFieldExpression>
                                </textField>
                            </cellContents>
                        </crosstabColumnHeader>
                        <crosstabTotalColumnHeader>
                            <cellContents mode="Opaque" style="Crosstab_CT">
                                <staticText>
                                    <reportElement x="0" y="0" width="60" height="20" uuid="0d9f9063-0033-47c2-b43e-b7ad3e3ace10"/>
                                    <box>
                                        <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    </box>
                                    <textElement textAlignment="Center" verticalAlignment="Middle"/>
                                    <text><![CDATA[Total]]></text>
                                </staticText>
                            </cellContents>
                        </crosstabTotalColumnHeader>
                    </columnGroup>
                    <measure name="amount_MEASURE" class="java.lang.Integer" calculation="First">
                        <measureExpression><![CDATA[$F{amount}]]></measureExpression>
                    </measure>
                    <crosstabCell width="60" height="20">
                        <cellContents mode="Opaque" style="Crosstab_CD">
                            <textField isBlankWhenNull="true">
                                <reportElement x="0" y="0" width="60" height="20" uuid="caadf17b-6d92-4198-be52-76f4a6a2c224"/>
                                <box>
                                    <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                </box>
                                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                                <textFieldExpression><![CDATA[$V{amount_MEASURE}]]></textFieldExpression>
                            </textField>
                        </cellContents>
                    </crosstabCell>
                    <crosstabCell width="60" height="20" columnTotalGroup="day">
                        <cellContents mode="Opaque" style="Crosstab_CT">
                            <textField isBlankWhenNull="true">
                                <reportElement x="0" y="0" width="60" height="20" uuid="6720356e-ab18-4166-b982-a13aca179150"/>
                                <box>
                                    <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                </box>
                                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                                <textFieldExpression><![CDATA[$V{amount_MEASURE}]]></textFieldExpression>
                            </textField>
                        </cellContents>
                    </crosstabCell>
                    <crosstabCell width="60" height="20" rowTotalGroup="item_id">
                        <cellContents mode="Opaque" style="Crosstab_CT">
                            <textField>
                                <reportElement x="0" y="0" width="60" height="20" uuid="bf18a1cc-77a5-4235-9020-975f821ba902"/>
                                <textFieldExpression><![CDATA[$V{amount_MEASURE}]]></textFieldExpression>
                            </textField>
                        </cellContents>
                    </crosstabCell>
                    <crosstabCell width="60" height="20" rowTotalGroup="item_id" columnTotalGroup="day">
                        <cellContents mode="Opaque" style="Crosstab_CT">
                            <textField>
                                <reportElement x="0" y="0" width="60" height="20" uuid="fdcd4b76-1077-423a-97ea-d9867f06c061"/>
                                <textFieldExpression><![CDATA[$V{amount_MEASURE}]]></textFieldExpression>
                            </textField>
                        </cellContents>
                    </crosstabCell>
                    <crosstabCell width="60" height="20" rowTotalGroup="name">
                        <cellContents mode="Opaque" style="Crosstab_CG">
                            <textField>
                                <reportElement x="0" y="0" width="60" height="20" uuid="047e6e2d-f599-48c9-8fc6-44b263ebdf5f"/>
                                <textFieldExpression><![CDATA[$V{amount_MEASURE}]]></textFieldExpression>
                            </textField>
                        </cellContents>
                    </crosstabCell>
                    <crosstabCell width="60" height="20" rowTotalGroup="name" columnTotalGroup="day">
                        <cellContents mode="Opaque" style="Crosstab_CT">
                            <textField>
                                <reportElement x="0" y="0" width="60" height="20" uuid="8acb77f7-b071-49da-bb01-c3410f49d802"/>
                                <textFieldExpression><![CDATA[$V{amount_MEASURE}]]></textFieldExpression>
                            </textField>
                        </cellContents>
                    </crosstabCell>
                </crosstab>
            </band>
        </groupHeader>
    </group>
</jasperReport>

叉子和勺子 DDL 和数据:

CREATE TABLE public.items (
    item_id int4 NOT NULL,
    "name" varchar(200) NOT NULL,
    description varchar(1000) NULL,
    CONSTRAINT items_pk PRIMARY KEY (item_id)
);

CREATE TABLE public.items_sales (
    item_id int4 NOT NULL,
    country varchar(100) NOT NULL,
    "day" int2 NOT NULL,
    amount int4 NULL,
    CONSTRAINT items_stat_pk PRIMARY KEY (item_id, country, day),
    CONSTRAINT items_stat_fk FOREIGN KEY (item_id) REFERENCES items(item_id) ON UPDATE CASCADE ON DELETE CASCADE
);

INSERT INTO public.items (item_id,"name",description) VALUES
     (1,'fork',NULL),
     (2,'spoon',NULL);

INSERT INTO public.items_sales (item_id,country,"day",amount) VALUES
     (1,'Italy',1,10),
     (1,'Italy',4,2),
     (1,'Italy',17,14),
     (1,'France',5,10),
     (1,'France',1,14),
     (2,'Italy',1,11),
     (2,'Italy',4,9),
     (2,'Italy',27,18),
     (2,'France',10,23),
     (2,'France',8,17);
INSERT INTO public.items_sales (item_id,country,"day",amount) VALUES
     (2,'France',1,7),
     (2,'France',23,11),
     (1,'France',10,37);     
jasper-reports ireport
© www.soinside.com 2019 - 2024. All rights reserved.