gorm


Grails Detached Criteria Query and “group by” and “having” clause


In a Grails 2.1 application, I'm having trouble getting a criteria query to behave like some handwritten sql I've got.
So-here's some background info:
The table is in a reporting db and has the following ddl:
completed_case table columns
id
completedDate
caseState
caseId (this field is not unique)
state
some other columns
customerName, other data columns
A row is added to this table whenever a value for that case changes, so there are often >1 rows per caseId.
I've been tasked with finding the first row for each case within a completedDate range. I've got some hacky sql that seems to get me the values I want:
select * from dbo.completed_case where id in
(select min(id)
from dbo.completed_case cci
group by case_id
having ((min(completed_date) > convert(datetime, 'Oct 19 2012 11:01AM', 100)) and
(min(completed_date) < convert(datetime, 'Oct 21 2012 11:01AM', 100)))) and
status = 'DIAGNOSED'
But I'm not sure how to translate this query into a Grails/Gorm CriteriaQuery. I was guessing the following(using a detached criteria to do the sub select), but it seems like DetachedCriteria doesn't have groupPropery() so I'm a bit lost.
Anyways, here's my guess that includes the not-valid call to groupProperty:
HibernateCriteriaBuilder criteria = CompletedCase.reports.createCriteria()
def results = criteria {
eqAll("id", new DetachedCriteria(CompletedCase).build {
projections {
min("id")
}
groupProperty("caseId") //XXX this method doesn't exist on DetachedCriteria
between("completedDate", startDate.toDate(), endDate.toDate())
})
eq("status", "DIAGNOSED")
fetchMode 'AssociatedTable', FetchMode.JOIN
}
Is there some way to generate this query using the interfaces provided by Gorm/Grails?
thanks
I just did it using hql.
select cci from CompletedCase as cci where id in (
select min(id) from CompletedCase group by caseId
) and cci.completedDate > :startDate and cci.completedDate < :endDate and cci.status = :status
order by cci.id

Related Links

“grails.mongodb.default.mapping” equivalent in application.yml in Grails 3.2
TenantId is null when saving an entity in a Discriminator based multi tenant application in Grails 3.2 with GORM 6
Grails 3 (unsaved) instance issue while fetching from database (GORM)
Can't use get() in domain using Grails3
GORM findAll + cannot pass dynamic List as named parameter
How to display database records in grails
Configure Grails 3.0.9 for Oracle 12c with identity PK?
Adding condition to findAll and findAllBy methods
Neo4j and Mongodb as datasource in Grails
GORM (varchar .save(), non DATE): ORA-01861: literal does not match format string
GORM addTo methods not found in Spock test
spring-boot property exception while externalizing datasource properties and using actuator
Mapping hasMany relation and saving the related data
Method on class [] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails Correctly
What happens when two app servers in cluster start LiquiBase update (via Grails)?
use of mappedBy in Many to Many relationship in Grails

Categories

HOME
c#-4.0
drivers
single-sign-on
platform-builder
fme
sd-card
google-docs
filtering
wamp
amazon-cloudformation
mapserver
export-to-csv
zend-framework3
gz
fallback
vaadin7
windows-7-x64
apache-metamodel
opennlp
selectedindexchanged
intel-pin
neo4j-spatial
compatibility
librato
tinymce-4
bcd
phonegap
airconsole
revolution-slider
unobtrusive-validation
temporary-files
android-tabhost
gzipstream
reportingservices-2005
janrain
midl
sencha-touch-2.3
angular2-meteor
wso2carbon
xml-documentation
chain-builder
qsslsocket
theming
pebble-watch
aurelia-validation
windows-iot-core-10
cudafy.net
flow-control
dwscript
django-scheduler
orthogonal
objective-c-swift-bridge
azure-sdk
httplistener
asp.net-4.5
google-web-starter-kit
nsfilemanager
autorest
holder.js
blackberry-10
php-parse-error
vhd
simple-framework
interrupted-exception
generic-programming
formatjs
modalpopup
offloading
tween
xc16
app42
intentservice
uv-mapping
oam
gridfs
django-nonrel
yui-compressor
lcs
type-equivalence
frameset
netbeans-6.9
asyncfileupload
hgsubversion
data-loss
gcj
datareader
w3wp.exe
dentrix
dbal
xetex
django-notification

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App