使用值从LEFT JOIN填充NULLS

时间:2013-01-09 16:11:41

标签: mysql sql database group-by left-join

SELECT

    rates_Calendar.date,
    subQuery.name,
    COALESCE(subQuery.amount,0) as amount,
    subQuery.reference,
    subQuery.property


FROM
    rates_Calendar
    LEFT JOIN (
                SELECT
                    rates_Booking.date,
                    unit.unit,
                    unit.abbreviation as name,
                    rates_Booking.amount,
                    rates_Booking.bookingReference AS reference,
                    property.property


                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

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

                WHERE
                    rates_Booking.date BETWEEN @startDate AND @endDate
                    AND unit.unit = 221



                GROUP BY
                    property.area,
                    property.property,
                    apartmentTypes.id,
                    unit.unit,
                    rates_Booking.date

                ) AS subQuery ON subQuery.date = rates_Calendar.date


 WHERE
     rates_Calendar.date BETWEEN @startDate AND @endDate
 GROUP BY 
subQuery.reference,
subQuery.unit,
subQuery.apartmentTypeId,
subQuery.property,
subQuery.area,
    rates_Calendar.date

现在,显然此查询将导致NULLS用于不匹配的日期。 有没有办法用NON NULL值更新所有NULLS?

2013-01-01  unitA 138 1      property1
2013-01-02  unitA 138 1      property1
2013-01-03  unitA 138 1      property1
2013-01-04  NULL  0   NULL   NULL
2013-01-05  NULL  0   NULL   NULL

有没有办法在相应列中使用NON NULLS更新NULL?

我正在尝试这样做,因为隐藏rowGroups与NULLS是不可能的,因为从链接可以理解:    Hide NULL Row Groups JasperReports

4 个答案:

答案 0 :(得分:1)

我认为您不希望LEFT JOIN使用subQuery。试试这个:

SELECT

    rates_Calendar.date,
    subQuery.name,
    COALESCE(subQuery.amount,0) as amount,
    subQuery.reference,
    subQuery.property


FROM
    rates_Calendar, (
                SELECT
                    rates_Booking.date,
                    unit.unit,
                    unit.abbreviation as name,
                    rates_Booking.amount,
                    rates_Booking.bookingReference AS reference,
                    property.property


                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

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

                WHERE
                    rates_Booking.date BETWEEN @startDate AND @endDate
                    AND unit.unit = 221



                GROUP BY
                    property.area,
                    property.property,
                    apartmentTypes.id,
                    unit.unit,
                    rates_Booking.date

                ) AS subQuery


 WHERE
   rates_Calendar.date BETWEEN @startDate AND @endDate

答案 1 :(得分:1)

这是你想要的吗?

SELECT rates_Calendar.date, coalesce(subQuery.name, 'unitA'),
        COALESCE(subQuery.amount,0) as amount,
        coalesce(subQuery.reference, 1),
        coalesce(subQuery.property, 'property1')
. . .

或者,您想要读取列中的值,例如:

select rates_Calendar.date,
       coalesce(subquery.Name, max(SubQuery.name) over ()) as name,
       COALESCE(subQuery.amount,0) as amount,
       coalesce(subQuery.reference, max(subquery.reference) over ()) as reference,
       coalesce(subQuery.property, max(subquery.property) over ()) as property

这将从列中获取最大值,并将其用作默认值。

答案 2 :(得分:0)

您可能需要IFNULL功能吗?

select ifnull(some_field, 'default_value') from mytable

答案 3 :(得分:0)

我设法通过使用CROSS JOIN ON rates_Calendar和单位表并加入这两个表上的子查询来实现这一目的。