使用 Order by expression 在交叉表排序 column/row 组中排序
Sorting in Crosstab sort column/row group with Order by expression
我知道这里已经有一个类似的线程,但它对我不起作用。
背景:Jaspersoft Studio 6
我有这样的数据集:
在此数据集上创建了这样的交叉表:
如何将列组排序为 prod c、prod a、prod b 而不是当前?
我尝试将此表达式按列组的表达式排序。
$F{product}.equals("prod c")? 1 : $F{product}.equals("prod a")? 2:3
但是它说
编辑:添加源,这是没有排序的版本,它运行良好没有错误,但当然没有排序。
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0 -->
<!-- 2016-06-08T14:14:48 -->
<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="test3" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="0a3345e5-7859-4366-9ccf-b215cf60a3b0">
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="vha"/>
<style name="Crosstab_CH" mode="Opaque" backcolor="#F0F8FF">
<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="#BFE1FF">
<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="#005FB3">
<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>
<queryString>
<![CDATA[select 'channel a' as "sales channel", 'prod a' as product, 1 as amount
union all select 'channel a' as salesChannel, 'prod b' as product, 1 as amount
union all select 'channel a' as salesChannel, 'prod c' as product, 1 as amount
union all select 'channel b' as salesChannel, 'prod a' as product, 1 as amount
union all select 'channel b' as salesChannel, 'prod b' as product, 1 as amount
union all select 'channel b' as salesChannel, 'prod c' as product, 1 as amount]]>
</queryString>
<field name="sales channel" class="java.lang.String"/>
<field name="product" class="java.lang.String"/>
<field name="amount" class="java.lang.Integer"/>
<background>
<band splitType="Stretch"/>
</background>
<summary>
<band height="224" splitType="Stretch">
<crosstab>
<reportElement x="91" y="24" width="459" height="200" uuid="66af64fd-5d36-48a9-9356-f1c2cd55f6a0">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
</reportElement>
<rowGroup name="sales channel1" width="60" totalPosition="End">
<bucket class="java.lang.String">
<bucketExpression><![CDATA[$F{sales channel}]]></bucketExpression>
</bucket>
<crosstabRowHeader>
<cellContents mode="Opaque" style="Crosstab_CH">
<textField>
<reportElement x="0" y="0" width="60" height="20" uuid="27883596-a403-4b9a-a3d5-d18adced1ec1"/>
<textFieldExpression><![CDATA[$V{sales channel1}]]></textFieldExpression>
</textField>
</cellContents>
</crosstabRowHeader>
<crosstabTotalRowHeader>
<cellContents mode="Opaque" style="Crosstab_CT">
<staticText>
<reportElement x="0" y="0" width="60" height="20" forecolor="#FFFFFF" uuid="7e48fc9b-d619-4b04-8b50-4fbd6174509e"/>
<text><![CDATA[Total sales channel1]]></text>
</staticText>
</cellContents>
</crosstabTotalRowHeader>
</rowGroup>
<columnGroup name="product1" height="20" totalPosition="End">
<bucket class="java.lang.String">
<bucketExpression><![CDATA[$F{product}]]></bucketExpression>
</bucket>
<crosstabColumnHeader>
<cellContents mode="Opaque" style="Crosstab_CH">
<textField>
<reportElement x="0" y="0" width="60" height="20" uuid="81d54f8e-11ab-4841-9aef-ef224899c337"/>
<textFieldExpression><![CDATA[$V{product1}]]></textFieldExpression>
</textField>
</cellContents>
</crosstabColumnHeader>
<crosstabTotalColumnHeader>
<cellContents mode="Opaque" style="Crosstab_CT">
<staticText>
<reportElement x="0" y="0" width="60" height="20" forecolor="#FFFFFF" uuid="0925adf9-e1d5-4b68-a7c5-0650b7e72721"/>
<text><![CDATA[Total product1]]></text>
</staticText>
</cellContents>
</crosstabTotalColumnHeader>
</columnGroup>
<measure name="amount_MEASURE1" class="java.lang.Integer" calculation="Count">
<measureExpression><![CDATA[$F{amount}]]></measureExpression>
</measure>
<crosstabCell width="60" height="20">
<cellContents mode="Opaque" style="Crosstab_CD">
<textField>
<reportElement x="0" y="0" width="60" height="20" uuid="cdbc8bf6-e55f-4e69-9505-d40d01870add"/>
<textFieldExpression><![CDATA[$V{amount_MEASURE1}]]></textFieldExpression>
</textField>
</cellContents>
</crosstabCell>
<crosstabCell width="60" height="20" columnTotalGroup="product1">
<cellContents mode="Opaque" style="Crosstab_CT">
<textField>
<reportElement x="0" y="0" width="60" height="20" forecolor="#FFFFFF" uuid="3543f652-7dee-43fd-84ba-514613b8ac03"/>
<textFieldExpression><![CDATA[$V{amount_MEASURE1}]]></textFieldExpression>
</textField>
</cellContents>
</crosstabCell>
<crosstabCell width="60" height="20" rowTotalGroup="sales channel1">
<cellContents mode="Opaque" style="Crosstab_CT">
<textField>
<reportElement x="0" y="0" width="60" height="20" forecolor="#FFFFFF" uuid="9657bd6f-c862-4462-b88f-6ea0e049a3ad"/>
<textFieldExpression><![CDATA[$V{amount_MEASURE1}]]></textFieldExpression>
</textField>
</cellContents>
</crosstabCell>
<crosstabCell width="60" height="20" rowTotalGroup="sales channel1" columnTotalGroup="product1">
<cellContents mode="Opaque" style="Crosstab_CT">
<textField>
<reportElement x="0" y="0" width="60" height="20" forecolor="#FFFFFF" uuid="8311db87-f185-447a-b692-ce878b7fdff5"/>
<textFieldExpression><![CDATA[$V{amount_MEASURE1}]]></textFieldExpression>
</textField>
</cellContents>
</crosstabCell>
</crosstab>
</band>
</summary>
</jasperReport>
当使用 Order By Expression 时,无法访问 Fields $F{}
,只能访问 measure变量(来自here):
The expression is evaluated in the context of the crosstab group and can reference measure variables, which evaluate to group totals.
编辑
测量变量 是在创建交叉表时预定义的。他们遵循一种模式(来自 here,“了解交叉表总变量”):
When you have multiple row or column groups, you can use crosstab total variables to combine data at different aggregation levels (for example, to calculate a percentage). The following built-in variables are available:
• <Measure>_<Column Group>_ALL
: The total of all the entries in the specified column group and the current row.
• <Measure>_<Row Group>_ALL
: The total of all the entries in the specified row group and the current column.
• <Measure>_<Row Group>_<Column Group>_ALL
: The combined total of all the entries in the specified row and column groups.
You can also select these variables from the expression editor for the Expression field on the Text Field tab of the Properties view for a measure.
据我了解,您可以使用这些变量对结果进行分组和排序。这些变量显示在交叉表的表达式编辑器中(源代码中有屏幕截图)。
如@tobi6 在他的 post 中所述,您可以在 orderByExpression 中使用 Measures(您还可以使用 Buckets with $V{} expression but it may be a hard way)
对于您的情况,最简单的方法是添加新的 Measure 和排序规则。
<measure name="productForSort" class="java.lang.Integer">
<measureExpression><![CDATA[$F{product}.equals("prod b") ? 1 : $F{product}.equals("prod c") ? 2 : 3]]></measureExpression>
</measure>
之后你可以在 orderByExpression 表达式中使用这个 Measure:
<bucket class="java.lang.String">
<bucketExpression><![CDATA[$F{product}]]></bucketExpression>
<orderByExpression><![CDATA[$V{productForSort}]]></orderByExpression>
</bucket>
我的测试csv 数据源:
sales channel, product, amount
channel a, prod c, 5
channel a, prod a, 1
channel b, prod a, 3
channel a, prod b, 1
channel b, prod b, 1
结果是这样的:
如您所见,"product b" 先行。
备注:
问题很相似(重复)一个非常棒的问题post Jasper Reports crosstab sorting with comparatorExpression。您可以在 post.
中找到更多信息
我的回答与@PetterFriberg 的solution 相似。但在这种情况下,我们可以省略 Comparator
的使用
另外两个答案也不错,特别是@AndreasDietrich的R&D
仅供参考,我无法(在 Jasper Studio 6.4 中)在 GUI 中从 Order By 下拉菜单中进行 select 测量,但是当我手动输入它时它可以工作GUI表达式框或直接进入xml.
创建变量来计算条件排序
<variable name="Data_Sort" class="java.lang.Double">
<variableExpression><![CDATA[($P{Variance}=="Positive" ? $V{Vol_Diff}:(($P{Variance}=="Negative") ? $V{Vol_DiffInverse} :$V{Vol_AbsDiff}))]]>
</variableExpression>
</variable>
在交叉表中创建了一个度量来访问排序变量
<measure name="DataSort_Measure" class="java.lang.Double">
<measureExpression><![CDATA[$V{Data_Sort}]]></measureExpression>
</measure>
在我的行组的 bucket 的 orderby 表达式中使用了 Measure 变量
<rowGroup name="Well.WellName" width="150">
<bucket order="Descending" class="java.lang.String">
<bucketExpression><![CDATA[$F{Well.WellName}]]></bucketExpression>
<orderByExpression><![CDATA[$V{DataSort_Measure}]]>
</orderByExpression>
</bucket>
我知道这里已经有一个类似的线程,但它对我不起作用。
背景:Jaspersoft Studio 6
我有这样的数据集:
在此数据集上创建了这样的交叉表:
如何将列组排序为 prod c、prod a、prod b 而不是当前?
我尝试将此表达式按列组的表达式排序。
$F{product}.equals("prod c")? 1 : $F{product}.equals("prod a")? 2:3
但是它说
编辑:添加源,这是没有排序的版本,它运行良好没有错误,但当然没有排序。
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0 -->
<!-- 2016-06-08T14:14:48 -->
<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="test3" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="0a3345e5-7859-4366-9ccf-b215cf60a3b0">
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="vha"/>
<style name="Crosstab_CH" mode="Opaque" backcolor="#F0F8FF">
<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="#BFE1FF">
<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="#005FB3">
<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>
<queryString>
<![CDATA[select 'channel a' as "sales channel", 'prod a' as product, 1 as amount
union all select 'channel a' as salesChannel, 'prod b' as product, 1 as amount
union all select 'channel a' as salesChannel, 'prod c' as product, 1 as amount
union all select 'channel b' as salesChannel, 'prod a' as product, 1 as amount
union all select 'channel b' as salesChannel, 'prod b' as product, 1 as amount
union all select 'channel b' as salesChannel, 'prod c' as product, 1 as amount]]>
</queryString>
<field name="sales channel" class="java.lang.String"/>
<field name="product" class="java.lang.String"/>
<field name="amount" class="java.lang.Integer"/>
<background>
<band splitType="Stretch"/>
</background>
<summary>
<band height="224" splitType="Stretch">
<crosstab>
<reportElement x="91" y="24" width="459" height="200" uuid="66af64fd-5d36-48a9-9356-f1c2cd55f6a0">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
</reportElement>
<rowGroup name="sales channel1" width="60" totalPosition="End">
<bucket class="java.lang.String">
<bucketExpression><![CDATA[$F{sales channel}]]></bucketExpression>
</bucket>
<crosstabRowHeader>
<cellContents mode="Opaque" style="Crosstab_CH">
<textField>
<reportElement x="0" y="0" width="60" height="20" uuid="27883596-a403-4b9a-a3d5-d18adced1ec1"/>
<textFieldExpression><![CDATA[$V{sales channel1}]]></textFieldExpression>
</textField>
</cellContents>
</crosstabRowHeader>
<crosstabTotalRowHeader>
<cellContents mode="Opaque" style="Crosstab_CT">
<staticText>
<reportElement x="0" y="0" width="60" height="20" forecolor="#FFFFFF" uuid="7e48fc9b-d619-4b04-8b50-4fbd6174509e"/>
<text><![CDATA[Total sales channel1]]></text>
</staticText>
</cellContents>
</crosstabTotalRowHeader>
</rowGroup>
<columnGroup name="product1" height="20" totalPosition="End">
<bucket class="java.lang.String">
<bucketExpression><![CDATA[$F{product}]]></bucketExpression>
</bucket>
<crosstabColumnHeader>
<cellContents mode="Opaque" style="Crosstab_CH">
<textField>
<reportElement x="0" y="0" width="60" height="20" uuid="81d54f8e-11ab-4841-9aef-ef224899c337"/>
<textFieldExpression><![CDATA[$V{product1}]]></textFieldExpression>
</textField>
</cellContents>
</crosstabColumnHeader>
<crosstabTotalColumnHeader>
<cellContents mode="Opaque" style="Crosstab_CT">
<staticText>
<reportElement x="0" y="0" width="60" height="20" forecolor="#FFFFFF" uuid="0925adf9-e1d5-4b68-a7c5-0650b7e72721"/>
<text><![CDATA[Total product1]]></text>
</staticText>
</cellContents>
</crosstabTotalColumnHeader>
</columnGroup>
<measure name="amount_MEASURE1" class="java.lang.Integer" calculation="Count">
<measureExpression><![CDATA[$F{amount}]]></measureExpression>
</measure>
<crosstabCell width="60" height="20">
<cellContents mode="Opaque" style="Crosstab_CD">
<textField>
<reportElement x="0" y="0" width="60" height="20" uuid="cdbc8bf6-e55f-4e69-9505-d40d01870add"/>
<textFieldExpression><![CDATA[$V{amount_MEASURE1}]]></textFieldExpression>
</textField>
</cellContents>
</crosstabCell>
<crosstabCell width="60" height="20" columnTotalGroup="product1">
<cellContents mode="Opaque" style="Crosstab_CT">
<textField>
<reportElement x="0" y="0" width="60" height="20" forecolor="#FFFFFF" uuid="3543f652-7dee-43fd-84ba-514613b8ac03"/>
<textFieldExpression><![CDATA[$V{amount_MEASURE1}]]></textFieldExpression>
</textField>
</cellContents>
</crosstabCell>
<crosstabCell width="60" height="20" rowTotalGroup="sales channel1">
<cellContents mode="Opaque" style="Crosstab_CT">
<textField>
<reportElement x="0" y="0" width="60" height="20" forecolor="#FFFFFF" uuid="9657bd6f-c862-4462-b88f-6ea0e049a3ad"/>
<textFieldExpression><![CDATA[$V{amount_MEASURE1}]]></textFieldExpression>
</textField>
</cellContents>
</crosstabCell>
<crosstabCell width="60" height="20" rowTotalGroup="sales channel1" columnTotalGroup="product1">
<cellContents mode="Opaque" style="Crosstab_CT">
<textField>
<reportElement x="0" y="0" width="60" height="20" forecolor="#FFFFFF" uuid="8311db87-f185-447a-b692-ce878b7fdff5"/>
<textFieldExpression><![CDATA[$V{amount_MEASURE1}]]></textFieldExpression>
</textField>
</cellContents>
</crosstabCell>
</crosstab>
</band>
</summary>
</jasperReport>
当使用 Order By Expression 时,无法访问 Fields $F{}
,只能访问 measure变量(来自here):
The expression is evaluated in the context of the crosstab group and can reference measure variables, which evaluate to group totals.
编辑
测量变量 是在创建交叉表时预定义的。他们遵循一种模式(来自 here,“了解交叉表总变量”):
When you have multiple row or column groups, you can use crosstab total variables to combine data at different aggregation levels (for example, to calculate a percentage). The following built-in variables are available:
•
<Measure>_<Column Group>_ALL
: The total of all the entries in the specified column group and the current row.•
<Measure>_<Row Group>_ALL
: The total of all the entries in the specified row group and the current column.•
<Measure>_<Row Group>_<Column Group>_ALL
: The combined total of all the entries in the specified row and column groups.You can also select these variables from the expression editor for the Expression field on the Text Field tab of the Properties view for a measure.
据我了解,您可以使用这些变量对结果进行分组和排序。这些变量显示在交叉表的表达式编辑器中(源代码中有屏幕截图)。
如@tobi6 在他的 post 中所述,您可以在 orderByExpression 中使用 Measures(您还可以使用 Buckets with $V{} expression but it may be a hard way)
对于您的情况,最简单的方法是添加新的 Measure 和排序规则。
<measure name="productForSort" class="java.lang.Integer">
<measureExpression><![CDATA[$F{product}.equals("prod b") ? 1 : $F{product}.equals("prod c") ? 2 : 3]]></measureExpression>
</measure>
之后你可以在 orderByExpression 表达式中使用这个 Measure:
<bucket class="java.lang.String">
<bucketExpression><![CDATA[$F{product}]]></bucketExpression>
<orderByExpression><![CDATA[$V{productForSort}]]></orderByExpression>
</bucket>
我的测试csv 数据源:
sales channel, product, amount
channel a, prod c, 5
channel a, prod a, 1
channel b, prod a, 3
channel a, prod b, 1
channel b, prod b, 1
结果是这样的:
如您所见,"product b" 先行。
备注:
问题很相似(重复)一个非常棒的问题post Jasper Reports crosstab sorting with comparatorExpression。您可以在 post.
中找到更多信息我的回答与@PetterFriberg 的solution 相似。但在这种情况下,我们可以省略 Comparator
的使用另外两个答案也不错,特别是@AndreasDietrich的R&D
仅供参考,我无法(在 Jasper Studio 6.4 中)在 GUI 中从 Order By 下拉菜单中进行 select 测量,但是当我手动输入它时它可以工作GUI表达式框或直接进入xml.
创建变量来计算条件排序
<variable name="Data_Sort" class="java.lang.Double"> <variableExpression><![CDATA[($P{Variance}=="Positive" ? $V{Vol_Diff}:(($P{Variance}=="Negative") ? $V{Vol_DiffInverse} :$V{Vol_AbsDiff}))]]> </variableExpression> </variable>
在交叉表中创建了一个度量来访问排序变量
<measure name="DataSort_Measure" class="java.lang.Double"> <measureExpression><![CDATA[$V{Data_Sort}]]></measureExpression> </measure>
在我的行组的 bucket 的 orderby 表达式中使用了 Measure 变量
<rowGroup name="Well.WellName" width="150"> <bucket order="Descending" class="java.lang.String"> <bucketExpression><![CDATA[$F{Well.WellName}]]></bucketExpression> <orderByExpression><![CDATA[$V{DataSort_Measure}]]> </orderByExpression> </bucket>