我在使用交叉表的报告中有两个查询联盟。 报告的目的是列出与每个日期对应的值。
1st Query返回与日期对应的值。 第二个查询返回剩余的日期及其值。
最后,完成两个查询的联合。 许多人可能会建议使用单个查询来实现此目的,但我无法使用LEFT JOIN进行单个查询,因为它不返回NULL值。 即使我得到了它,我所拥有的GROUP BY子句将在iReport中分成两行.NON NULLS和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)。 Date是columnGroup。 理想情况下,我希望这是一排。
查询
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条件没有帮助。
答案 0 :(得分:1)
您可以在交叉表详细信息字段中添加它们,而不是在查询中添加0.00值,修改详细信息/详细信息部分中字段的字段表达式:
$V{amountMeasure} == null ? 0.00 : $V{amountMeasure}
假设$V{amountMeasure}
是基于金额字段生成的度量名称。
答案 1 :(得分:0)
SELECT DATE, SUM(AMOUNT)
FROM (first query UNION second query)
GROUP BY DATE
ORDER BY DATE