Gorm count elements group by day without time - grails

I wish to retrieve a list of UserProfile registrations per day.
The domain object UserProfile stores a Date creationDate property.
I've tried
def results = UserProfile.executeQuery('select u.creationDate, count(u) from UserProfile as u group by u.creationDate')
println results
which obviously is not what I need because data is (already) stored with complete time in it.
Any resource savvy solution will fit: projections, hql, ...
Thnks

I use HQL cast function:
def results = UserProfile.executeQuery("""
select cast(u.creationDate as date), count(u)
from UserProfile as u
group by cast(u.creationDate as date)
""")
Underlying database must support ANSI cast(... as ...) syntax for this to work, which is the case for PostgreSQL, MySQL, Oracle, SQL Server and many other DBMSs

Break down the date to day, month and year then ignore the timestamp.
This should give you what you need.
def query =
"""
select new map(day(u.creationDate) as day,
month(u.creationDate) as month,
year(u.creationDate) as year,
count(u) as count)
from UserProfile as u
group by day(u.creationDate),
month(u.creationDate),
year(u.creationDate)
"""
//If you do not worry about dates any more then this should be enough
def results = UserProfile.executeQuery( query )
//Or create date string which can be parsed later
def refinedresults =
results.collect { [ "$it.year-$it.month-$it.day" : it.count ] }
//Or parse it right here
def refinedresults =
results.collect {
[ Date.parse( 'yyyy-MM-dd', "$it.year-$it.month-$it.day" ) : it.count ]
}

You could define a "derived" property mapped as a formula to extract the date part of the date-and-time. The exact formula will differ depending what DB you're using, for MySQL you could use something like
Date creationDay // not sure exactly what type this needs to be, it may need
// to be java.sql.Date instead of java.util.Date
static mapping = {
creationDay formula: 'DATE(creation_date)'
}
(the formula uses DB column names rather than GORM property names). Now you can group by creationDay instead of by creationDate and it should do what you need.
Or instead of a "date" you could use separate fields for year, month and day as suggested in the other answer, and I think those functions are valid in H2 as well as MySQL.

Related

Mule 4 - Set Start Date & End Date as QueryParams

I'm having trouble defining both start and end dates as a query parameters. When the data gets pulled, it needs to return as a range of dates based on the query parameters. The GET URL would look like http://localhost:8081/test?FileType=Sales&StartDate=2022-10-01&EndDate=2022-10-26. This should return a date range of data from 10/1/2022-10/26/2022.
In my query, my where clause is set to:
where dp.Nid = 405 and fs.DDate=:DDate
**dp and fs are used in my joins and 405 is an ID that i'll need to unique identify a product.
My input Parameters:
{ DDate : attributes.queryParams.StartDate, DDate : attributes.queryParams.EndDate }
What do i need to set to make a range of dates? Do i need to set startdate to > and enddate to < ? Also, is it possible to define query parameters when using a stored procedure instead of select database method in anypoint studio?
Operations in Mule 4 (ie the boxes inside a flow) can have several inputs (payload, variables, attributes) and 1 output, but they are expected to be independent from each other. The Database query operation doesn't care if its inputs come the query params or from somewhere else. You need to map inputs explicitly to parameters in the query.
Once you have the arguments you need to use them in the SQL query. Usually that means adding a greater than and a lesser than comparison, to ensure that the value is in range. Or the same including also equals, if the business logic requires it.
Depending on the data types and the SQL dialect you may need to convert the inputs to a date format that is compatible with the database type of the column. The inputs here are strings, because that's what query params always are parsed to. The column type is something that you will need to understand and see how to transform, in DataWeave or in the SQL query.
As an example:
<db:select config-ref="dbConfig">
<db:sql>SELECT ... WHERE dp.Nid = 405 AND fs.DDate >= :StartDate AND fs.DDate <= :StartDate</db:sql>
<db:input-parameters>
#[{
StartDate : attributes.queryParams.StartDate,
EndDate : attributes.queryParams.EndDate
}]
</db:input-parameters>
</db:select>

How to get records created in each month in rails

Looking for a way to get all the records that are created in each month for a table
For example i need to know how to get a result like:
January: 6,
Feb: 9,
March: 10
Ideally i'm looking at using the created_at field in the database to compare against.
You can use GROUP BY and COUNT from within SQL to efficiently retrieve the data. Rails offers various options here to build an SQL query which performs aggregations and calculations with ActiveRecord::Calculations.
Assuming you have a model named Record for your records and you use MySQL / MariaDB for your database, this can be used to get the number of records per month:
records_per_month = Record.group('EXTRACT(YEAR_MONTH FROM created_at)').count
This will return a hash of Integers (corresponding to the year and month of the group so that e.g. records in May 2022 will groups under the key 202205) and the number of records within this month as values.
From your example, this would be
{
202201 => 6,
202202 => 9,
202203 => 10
}
If desired, you can then further "format" the keys, e.g.
records_per_month.transform_keys! do |year_month|
Date.strptime(year_month.to_s, '%Y%m').strftime('%B %Y')
end
Here, we parse year-month integer as a date with Date.strptime and format the date with Date#strftime to show the month name and year, e.g. "February 2022".
Imagine you have a Users table (my Rails application has one), like this:
id
name
.
.
.
created_at
updated_at
You could use this code, which would return a hash of months with the count:
users = User.all
users.group_by {|u| u.created_at.strftime("%B")}.transform_values {|v| v.count}
Returns something like:
{"September"=>33,
"August"=>1,
"October"=>1,
"February"=>55,
"January"=>185,
"May"=>4,
"December"=>145,
"June"=>8,
"November"=>19,
"March"=>51,
"April"=>27,
"July"=>5}
Explanation
created_at.strftime("%B")
This converts the date to a Month, using strftime
users.group_by {|u| u.created_at.strftime("%B")}
Creates a hash that groups the user records by the Month name, using group_by
.transform_values {|v| v.count}
Instead of a collection of records, we just want the count. We leave the key alone in the hash, and use transform_values to count the values.

How to compare nested attributes on Grails with attributes from "outside" of the nested scope using named query?

I have a situation where I need to validate a range of dates from a nested object based on the date of my object.
So let say I have one class named:
Course{
Date date
Teacher teacher
}
Teacher{
Date effectiveDate
Date terminationDate
}
What I need to do is a query with a projection of another class and into the query I need to validate the date like this:
le 'effectiveDate', date
or {
ge 'terminationDate', date
isNull 'terminationDate'
}
The issue is that I can't use date because on the named query I don't have an instance of the object. How can I compare nested object attributes against this date?
I think what you want is to use createAlias. Unfortunatly, createAlias doesn't work with Spock unit testing, so I can't create a test to see if this works and I don't have time right now to create an app to test this, so this is mostly a guess.
Your Named Query should look something like this:
findCoursesWithActiveTeacher { ->
createAlias 'teacher', 't'
geProperty 'date', 't.effectiveDate'
or {
leProperty 'date', 't.terminationDate'
isNull 't.terminationDate'
}
}
I think I inverted the greater than and less then comparisons properly. Hopefully we get lucky and this works for you.

how to compare string with sql date in grails

I have written query like this
Rule.findAll("FROM Rule WHERE client_id = ? " +
"AND DATE_FORMAT(contract_begins,'%d-%m-%Y') <= ?", [tripStartDate])
But it is not able to compare the sql date with string tripStartDate, how to solve this problem ,how to check contract_begins is less than or equal to tripStartDate.
The easiest way would be to construct a new data object from your string to pass into the query. Assuming (I may be misunderstanding) contract_begins is mapped as a date type in your Rule
Date tripStart = new SimpleDateFormat("dd-MM-yyyy").parse(tripStartDate)
Rule.findAllByClientAndContractBeginsLessThanEquals(client, tripStart)
or if you really want to keep the hql rather than dynamic finder
Date tripStart = new SimpleDateFormat("dd-MM-yyyy").parse(tripStartDate)
Rule.findAll("FROM Rule WHERE client_id = :clientId AND contract_begins <= :tripStart",
[clientId: client.id, tripStart: tripStart])

Search records having comma seperated values that contains any element from the given list

I have a domain class Schedule with a property 'days' holding comma separated values like '2,5,6,8,9'.
Class Schedule {
String days
...
}
Schedule schedule1 = new Schedule(days :'2,5,6,8,9')
schedule1.save()
Schedule schedule2 = new Schedule(days :'1,5,9,13')
schedule2.save()
I need to get the list of the schedules having any day from the given list say [2,8,11].
Output: [schedule1]
How do I write the criteria query or HQL for the same. We can prefix & suffix the days with comma like ',2,5,6,8,9,' if that helps.
Thanks,
Hope you have a good reason for such denormalization - otherwise it would be better to save the list to a child table.
Otherwise, querying would be complicated. Like:
def days = [2,8,11]
// note to check for empty days
Schedule.withCriteria {
days.each { day ->
or {
like('username', "$day,%") // starts with "$day"
like('username', "%,$day,%")
like('username', "%,$day") // ends with "$day"
}
}
}
In MySQL there is a SET datatype and FIND_IN_SET function, but I've never used that with Grails. Some databases have support for standard SQL2003 ARRAY datatype for storing arrays in a field. It's possible to map them using hibernate usertypes (which are supported in Grails).
If you are using MySQL, FIND_IN_SET query should work with the Criteria API sqlRestriction:
http://grails.org/doc/latest/api/grails/orm/HibernateCriteriaBuilder.html#sqlRestriction(java.lang.String)
Using SET+FIND_IN_SET makes the queries a bit more efficient than like queries if you care about performance and have a real requirement to do denormalization.

Resources