3
votes

I asked an earlier question today which was about GORM: How to fetch records in grails by max date and group at same time However, someone suggested it can be easily achieved using HQL. But using HQL I'm getting unexpected token error. When I researched this I found out that HQL doesn't allow INNER JOINS unless there is an association between two entities: HQL, left join on the same table

So, I'm lost. To begin with, I'm frustrated why such a simple query isn't supported by GORM and now with HQL my question is: How do I perform an INNER JOIN on a subset?

What I've tried:

unexpected token: ( near line 1, column 16 [ select c from ( select name, max(dateCreated) as maxTime from com.mine.Color group by name ) as t inner join Color c on c.name = t.name and c.dateCreated = t.maxTime ]

I suspected that the second instance of Color was not being detected since the package name didn't get automatically prefixed to it. So reading other answers I tried:

unexpected token: ( near line 1, column 16 [ select c from ( select name, max(dateCreated) as maxTime from com.mine.Color group by name ) as t , com.mine.Color as c on c.name = t.name and c.dateCreated = t.maxTime ]

1
I think I have something for you, hold onJames Kleeh

1 Answers

2
votes

Here you go:

Color.executeQuery("""    
    Select c
    From Color c
    where c.dateCreated = (select max(b.dateCreated) from Color b where b.name = c.name)
    """)