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

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
Domain class auto validation in grails
Grails Detached Criteria Query and “group by” and “having” clause
Script for saving large no of entries in mongodb
Does the multiple datasource feature of Grails 2.0 support relations?
How to avoid ImprovedNamingStrategy in joinTable in Grails
Grails 2 Show transient property that is an alias for id column
How to save GORM class with composite id made from its own field?
Grails - 2 domains and 1 form create/update/edit issues
Is there a belongsTo mixed syntax for GORM many-to-many and back-reference
Why do these Gradle tests throw exceptions?
In grails 2 when using multiple datasources, can I specify which datasource a criteria should use?
GORM methods are not accessible in Domain and Service classes

Categories

HOME
gitlab
testng
openmp
zeromq
openstack
mediawiki
hp-exstream
onedrive
office365api
spagobi
jsp-tags
paw-app
google-project-tango
installshield
angular-ui-bootstrap
serverspec
ios-charts
append
session-variables
maude-system
etl
decomposition
vaadin7
google-pagespeed
web-sql
tapestry
lcd
microsoft-sync-framework
react-css-modules
nhapi
elasticsearch-net
kendo-datasource
delicious-api
binary-data
semantic-versioning
exuberant-ctags
oracle-fusion-middleware
column-family
http-referer
y86
normal-distribution
adobe-premiere
ansible-playbook
firebase-admin
flashair
cubic-spline
wso2carbon
fault
return-value
dotnetzip
slickedit
sybase-asa
boost-preprocessor
pdfclown
topbeat
pintos
jquery-filter
angular-cache
grails-tomcat-plugin
azure-sdk
android-textview
database-optimization
fuzzy-search
simplewebrtc
ubuntu-10.04
skype4py
bluemix-app-scan
oauth2client
goose
revolution-r
wapiti
retina
libressl
varargs
event-bubbling
sailfish-os
key-management
geonetwork
operator-precedence
ng-animate
xc16
camanjs
fpml
codeigniter-routing
nsbutton
client-side-templating
neolane
dml
undefined-reference
file-locking
anonymous-methods
hamiltonian-cycle
isnullorempty
propertyeditor
wse3.0
buildr
work-stealing
nsdatecomponents
gwt-ext
radcombobox
ext3
web-architecture
audio-capture
swing-app-framework
simpletest

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