Grails Model Count Number of occurence group by Date month, hour - date

I have a Grails Domain that contain two date feild
Date updated
Date created
I want to count number of rows based on updated time with respect to month or hour or year. How can I do that. The methods showed on Internet does not work. I am using grails version 2.4.4.
Update:
Example
I have a domain that contain four feids
Date Created
Date updated
Long id
Long idx
I want to find how many idx is there in every group when i group by hour. Like there can be 5 in 12h and 6 in 13th hour... so on.. How can i find those.... My comaparison is not fixed. It can be changed to mont or year... In year it will show the number of idx in each year. in month it will show the number of idx in each month..
How can I do this with grails...

You can use where detached query:
DomainClass.where { year( updated ) == 2014 }.count()
Similarly, hour(), minute() etc.
Refer WHERE queries for details.

Related

Tableau Fixed Max date registered by month

I want to create a graph line in tableau with a count distinct id per month.
I use an excel spreadsheet that is updated a few times per month but never on the same date, so I have different dates registered as months go by.
I want to use the last date registered per month so I can use that date to show month's trend through the years. Each id represent a different construction building, so it is expected that the same id can be found on different months.
This is what I tried: Tableau Fixed (LOD) formula the get last date registered per each month:
{ FIXED [id_constructionbuilding], MONTH([date_registered]): MAX([date_registered])}
Then I use rows and columns in tableau but I get more than one max date (my excel has 2020 and 2021 dates, so it's picking dates from October 2020 and October 2021 for example). Dates are order according to Spanish so you will see month and days shifted:
I would suggest updating your calculated field to:
{ FIXED [id_constructionbuilding],DATETRUNC('month', [date_registered]), DATETRUNC('year', [date_registered]):MAX([date_registered])}
This will keep the level of detail (LOD) to the id_constru..., month and year.

Mongodb query to retrieve data in month range

Does anyone know how to retrieve data by month using Mongodb query? For example, if I only want to retrieve the date range for the past month, or for the past two month, how do I do that? I see examples for getting data for the last N day (e.g for past 5 days using this new Date(new Date().setDate(new Date().getDate()-5) or N hours, but not seeing examples for only retrieving data for the last N month. Thanks,
Yes, You can retrieving data based on month like this.
new Date().setMonth(new Date().getMonth()-1) or N months.
And you can also get past month start and end date using moment like this.
moment().subtract(1,'months').startOf('month').format('YYYY-MM-DD')
it will provide previous month start date i.e 2021-01-01
moment().subtract(1,'months').endOf('month').format('YYYY-MM-DD');
it will provide previous month end date i.e 2021-01-31

Extract highest date per month from a list of dates

I have a date column which I am trying to query to return only the largest date per month.
What I currently have, albeit very simple, returns 99% of what I am looking for. For example, If I list the column in ascending order the first entry is 2016-10-17 and ranges up to 2017-10-06.
A point to note is that the last day of every month may not be present in the data, so I'm really just looking to pull back whatever is the "largest" date present for any existing month.
The query I'm running at the moment looks like
SELECT MAX(date_col)
FROM schema_name.table_name
WHERE <condition1>
AND <condition2>
GROUP BY EXTRACT (MONTH FROM date_col)
ORDER BY max;
This does actually return most of what I'm looking for - what I'm actually getting back is
"2016-11-30"
"2016-12-30"
"2017-01-31"
"2017-02-28"
"2017-03-31"
"2017-04-28"
"2017-05-31"
"2017-06-30"
"2017-07-31"
"2017-08-31"
"2017-09-29"
"2017-10-06"
which are indeed the maximal values present for every month in the column. However, the result set doesn't seem to include the maximum date value from October 2016 (The first months worth of data in the column). There are multiple values in the column for that month, ranging up to 2016-10-31.
If anyone could point out why the max value for this month isn't being returned, I'd much appreciate it.
You are grouping by month (1 to 12) rather than by month and year. Since 2017-10-06 is greater than any day in October 2016, that's what you get for the "October" group.
You should
GROUP BY date_trunc('month', date_col)

How to return the last day of each month

I am creating a view where i have multiple records showing up for each month.
Example: January is showing 20 records and February is showing 30 records. I only want the last record of EACH MONTH to show up. So i want to see 1 record for each month.
I have already tried the first() and last() table calculation, but it does not filter by month, but by column.
If you just apply first and last on the dataaset then you won't get the correct output, If you need for every month then you need to divide the partition to year and month and then apply the max on the specific partition.
Try this way:
Place the order date in Exact date format and change the property to discrete
Now extract the year and month in separate calculated fields and place in detail.
Year:
year(Date)
Month:
Month(date)
Now create one more calculated field and write below code:
WINDOW_MAX(MAX([Order Date]),FIRST(),LAST())
Try This
1 Calculated Field - DateMonth
Datetrunc('month',Date)
2 Calculative field - LastDate
{ FIXED DateMonth: max(Date)}
3 Calculative Field- Filter
if lastDate=Date then 'Yes' else 'No' end

Last 7 days data calculation in Tableau

I am having an issue in calculating last week data in Tableau. Below is my scenario:
In my dashboard, I have a slider which selects the date. In my table, I have a list of users where I will be showing each of them's call records. One column will have last week records and one will have total records.
For Total records, there is not an issue. But for finding last week's count, I need to have a calculated field, that needs to subtract 7 days from the date selected and then give out the number of records for each user.
Say I have selected date as 25-04-2017, then my table should show all the records until 25-04-2017 in one column and other should show data from 18-04-2017 till 25-04-2017.
You can filter it with Relative days. When adding your dimension (date type) to the Filters list the picture below will appear.
Now you can click on the relative date and to choose the best option for you. You can see it in the picture below.
Create a Date parameter for your user to select. Using the Superstore data set, I created a calc field for last 7 days sales:
if datediff('day',[Order Date],[date]) <= 7 and datediff('day',[Order Date],[date]) >= 0 then [Sales] end
And sales up to date:
if datediff('day',[Order Date],[date]) >= 0
then [Sales] end
See attached example: https://www.dropbox.com/s/nqdp9zj74jay72d/170427%20stack%20question.twbx?dl=0
I was able to find the solution for my issue.
I created a boolean field as Max7Days with the formula as below:
DATEDIFF('day', [Date] , {MAX([Date])} ) <= 7
And created another that would count the number of records for the last 7 days if the condition was true as per the below formula:
CASE [Max7Days]
WHEN TRUE
THEN
[Number of Records]
END