I want to convert following native sql query to hql query. I gone through hibernate docs they given examples on queries with distinct objects/fields but no where given about “distinct on” usage. Can some one help me to resolve this.
my sql query is :
select distinct on(mr.id) mr.*
from rebate.mr_rebates mr
where mr.mr_start_date <= now()
order by mr.id, mr.mr_start_date desc
Thanks.
Well
distinct onseems to be a database specific extension and thus is not standard SQL. Because of this, HQL can’t provide an exact equivalent (how would you automatically translate that on databases that don’t support that feature?).So, your best bet might be to change your query to use standard SQL syntax only. For some hints have a look here:
http://www.postgresonline.com/journal/archives/4-Using-Distinct-ON-to-return-newest-order-for-each-customer.html