I have a Union of two queries in a report using crosstab.
The purpose of the reports is to list the values corresponding to each date.
1st Query returns the values corresponding to a date.
2nd Query returns the remaining dates and their values.
Finally, Union of both the queries is done.
Many people might suggest to achieve this using a single query but I have been unable to do this with a single query using LEFT JOIN as it does not return the NULL values.
Even if I get that working, the GROUP BY clause that I have will separate into two rows in iReport.. NON NULLS AND NULLS.
So, the only way I can think of doing this is using a Union.
The output looks like
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 .
The resulting two rows is because of the GROUP BY.(rowGroup).
Date is the columnGroup.
Ideally, I would like this to be in a single row.
The query is
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}
Also, can someone suggest so that it does not need a UNION.
I know using the subquery will not result in optimal performance.
But having the WHERE conditions in the ON clauses in JOINS does not help.
1 Answer