简体   繁体   中英

How i can convert this mysql statement to hql

How i can convert this mysql statement to hql

    select invisit, notinVisit
    FROM(
    select COUNT(*) as invisit  from consultations cons join patients p 
    on cons.patient_id = p.id
    where cons.datefin > now()) as A
    JOIN 
    ( select COUNT(*) as notinVisit from consultations cons join patients p 
    on cons.patient_id = p.id
    where cons.datefin < now()) as B

my try was

"select com.organ.project.service.DTO.PatientsByInConsultationDTO(invisit, notinVisit)\n" +
            "FROM(\n" +
            "select COUNT(*) as invisit  from Consultation cons join Patient p \n" +
            "on cons.patient.id = p.id\n" +
            "where cons.datefin > now()) as A\n" +
            "JOIN \n" +
            "( select COUNT(*) as notinVisit from Consultation cons join Patient p \n" +
            "on cons.patient.id = p.id\n" +
            "where cons.datefin < now()) as B\n"

but i got this error:

 ...org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 2, column 5...

turned out that hql does not support subqueries

ps: i need the invisit , notinVisit props as constructor params to return an object containing the result

Try to use this

select new com.organ.project.service.DTO.PatientsByInConsultationDTO(
       sum(case when cons.datefin > now() then 1 else 0 end) as invisit,
       sum(case when cons.datefin < now() then 1 else 0 end) as notinVisit) 
from Consultation cons join Patient p on cons.patient.id = p.id
group by cons.id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM