Spring 数据 JPA - 如果参数在条件之间具有空值,则忽略该参数

Spring Data JPA- ignore parameter if it has a null value in between condition

我正在尝试编写以下查询

@Query(value = " " + "SELECT new om.gov.moh.ereferral.model.dto.ClinicalReferralStatisticsDto("
            + "SUM(CASE WHEN B.id = 1 THEN 1 END) AS OPD," + "SUM(CASE WHEN  B.id = 2 THEN 1 END)     AS IPD,"
            + "SUM(CASE WHEN B.id = 3 THEN 1 END)     AS DC," + "SUM(CASE WHEN B.id = 4 THEN 1 END )    AS PROC,"
            + " SUM(CASE WHEN B.id = 5 THEN 1 END)     AS SURGERY , SUM (CASE WHEN B.id IN (1,2,3,4,5)THEN 1 END)     AS Total)"
            + "FROM RefTbAppTransaction A, RefVwVisitType B "
            + "WHERE A.reqVisitType.id = B.id AND A.toEstCode = :toEstCode "
            + "AND A.referralDate BETWEEN TRUNC(:fromDate) AND TRUNC(:toDate) " 
            + "and (COALESCE(:fromEstCode, null) is null or A.fromEstCode in :fromEstCode)"
            )

现在它工作正常但我想要如果 fromDate 为 null 将所有数据带到 toDate 并且如果 toDate 为 null 将 fromDate 中所选日期的所有数据带到当前日期,如果两者都为 null 则忽略条件

您可以尝试下一个模式

where (:fromDate is null or A.referralDate >= :fromDate)
and (:toDate is null or A.referralDate <= :toDate)