【问题标题】:JasperReports club union result into a single rowJasperReports 俱乐部联盟结果成单行
【发布时间】:2013-01-09 19:41:41
【问题描述】:

我在使用交叉表的报告中有两个查询的联合。 报告的目的是列出每个日期对应的值。

第一个查询返回与日期对应的值。 第二个查询返回剩余日期及其值。

最后,两个查询的联合完成了。 许多人可能会建议使用单个查询来实现这一点,但我无法通过使用 LEFT JOIN 的单个查询来做到这一点,因为它不返回 NULL 值。 即使我得到了那个工作,我拥有的 GROUP BY 子句将在 iReport 中分成两行.. NON NULLS AND NULLS。

所以,我能想到的唯一方法是使用联合。 输出看起来像

      01/01/2013 02/01/2013 03/01/2013 04/01/2013 05/01/2013
X     £120.00    £120.00    £120.00                         
                                       £0.00     £0.00           .

结果两行是由于 GROUP BY.(rowGroup)。 日期是列组。 理想情况下,我希望它在一行中。

查询是

SELECT
   rates_Booking.date
   rates_Booking.amount,    
   booking.reference    

FROM
   rates_Booking
   LEFT JOIN booking ON booking.reference = rates_Booking.bookingReference                                                                  

   LEFT JOIN unit ON booking.apartment = unit.unit

   LEFT JOIN property ON property.property = unit.property

 WHERE
   rates_Booking.date BETWEEN $P{startDate} AND $P{endDate} 

   AND  $X{IN,property.property,buildings}
   AND  $X{IN,property.area,location}
   AND  $X{IN, unit.unit, unit}

UNION


   SELECT
    rates_Calendar.date
    0.00 as amount,
    '' as reference     

FROM
    rates_Calendar,unit


    LEFT JOIN property ON property.property = unit.property
    LEFT JOIN regions ON regions.region = property.area

    # unit to apartments
    LEFT JOIN apartments ON (apartments.unit = unit.unit)
    LEFT JOIN apartmentTypes ON (apartmentTypes.id = apartments.apartmentTypeId)


WHERE
    rates_Calendar.date BETWEEN $P{startDate} AND $P{endDate}
    AND rates_Calendar.date NOT IN (
                                       SELECT   
                                          rates_Booking.date
                                       FROM                                                                           
                                          rates_Booking                                                                             
                                          LEFT JOIN booking ON booking.reference =   rates_Booking.bookingReference                                 
                                          LEFT JOIN unit ON booking.apartment = unit.unit
                                          LEFT JOIN property ON property.property = unit.property                                                                               

                                          LEFT JOIN apartments ON (apartments.unit =  unit.unit)                                                                                                                         
                                          LEFT JOIN apartmentTypes ON (apartmentTypes.id = apartments.apartmentTypeId)

                                        WHERE                                                                            
                                           rates_Booking.date BETWEEN $P{startDate} AND $P{endDate}                                                                                                                                                         
                                           AND  $X{IN, unit.unit, unit}                                                                                                 
                                           AND  $X{IN, apartmentTypes.id,apartmentType}

                                        GROUP BY

                                           rates_Booking.date



                                            )                                                                                        



    AND $X{IN,property.property,buildings}
    AND $X{IN,property.area,location}
    AND $X{IN, unit.unit, unit}

另外,有人可以建议它不需要UNION。 我知道使用子查询不会带来最佳性能。 但是在 JOINS 的 ON 子句中使用 WHERE 条件并没有帮助。

【问题讨论】:

  • 我已更改主帖并现在包含查询。
  • 正如我所说,这不是理想的方式。我将其更改为在 LEFT JOIN 中使用子查询,这意味着我不再需要使用 UNION。
  • 但问题仍然存在.. 有没有办法可以把它排成一行?

标签: mysql jasper-reports union ireport crosstab


【解决方案1】:

除了在查询中添加 0.00 值,您还可以将它们添加到交叉表详细信息字段中,修改详细信息/详细信息部分中字段的字段表达式:

$V{amountMeasure} == null ? 0.00 : $V{amountMeasure}

假设$V{amountMeasure} 是根据金额字段生成的度量名称。

【讨论】:

    【解决方案2】:
     SELECT DATE, SUM(AMOUNT)
     FROM (first query UNION second query)
     GROUP BY DATE
     ORDER BY DATE
    

    【讨论】:

    • 顺便说一句,如果您只需要一个查询,我在 Oracle 中使用分区外连接 (oracle.com/technetwork/testcontent/outerjoin-097144.html) 执行类似操作。也许更熟悉 MySQL 的人知道 MySQL 是否也可以做到这一点?
    • 感谢丽莎。我已经这样做了。但已删除 UNION 以提高查询效率。
    猜你喜欢
    • 1970-01-01
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 2022-01-20
    • 1970-01-01
    相关资源
    最近更新 更多