What is the best way to group dates by day or month in solr queries ?
For example if I have the date in the following format: 20140108180854746 (actually the format doesn't matter all that much here, might as well be in a normal format), is there an easy way to just group by its days or months.
Someone suggested keeping a field just for the days that it will group by and the other solution I saw was something in the lines of
&group=true&group.func=rint(div(ms(date_dt),mul(24,mul(60,mul(60,1000)))))
which isn't exactly user friendly but it will do the trick I guess.
Any general advice about that?
There are in-built faceting methods to accomplish this. You can use the "facet.date.gap" param to achieve this. Take a look at the following example.
q=solr&facet=true
&facet.date=j_createddate
&facet.date.gap=+1DAY,+2DAY,+3DAY,+10DAY
Here, we define date faceting for j_createddate, a date field. Then we create compartments for the gaps 1 days, 2 days, 3 days and 10 days
Refer http://solr.pl/en/2010/08/23/the-scope-of-solr-faceting/ & http://wiki.apache.org/solr/SimpleFacetParameters#facet.date.gap
Related
I have to create a query in which I have to find the last workingday in the previous month. I know how to do this with my date dimension that has an IsWorkingday flag and joining to the date one month ago from today and from there find the max(dateid).
While I was doing this I was thinking shouldn't there be a more elegant solution like using LAG() with a specific partition by and order by.
I played with it for a while but couldn't find a solution. Any ideas?
That's not really what LAG() is for. It's better suited for things like calculating a running total. Please google the LAG() function in SQL Server if you want a better understanding.
A possibly more elegant solution would be to get the first working day of the current month, and select the first working day before it.
Without knowing your table structure and data, it's hard to be more specific.
I need to know the week of year of a timestamp using Derby/JavaDB. And to make it worse I have to extract it from a parameter not a column. Don't ask why but that should be a minor problem.
On DB2 I say someting like
VALUES WEEK_ISO(?)
But how do I get the same result with Derby aka JavaDB?
Thanks Bryan, your Link was very helpful.
http://therealdanvega.com/blog/2010/02/17/creating-apache-derby-custom-functions-part-2
It seems there is no week function in derby but it is very easy to program such a function yourself. Especially when you use it from Java.
While building a Calendar table with PowerQuery for a PowerPivot model in Excel 2013 I use its Date.QuarterOfYear function to get the number of the quarter.
Building the same thing in SSAS Tabular requires some workarounds. There's no equivalent DAX function to get number of the quarter from a DATE. Strangely, DAX has YEAR() and MONTH(), but no QUARTER().
Are nested IF or SWITCH statements really the only way in DAX to get the quarter number?
What is the reason for the absence such a simple and useful function? Am I overlooking the supreme wisdom of this decision?
I found an answer in this great book!
One should use =ROUNDUP(MONTH([Date])/3, 0) to get quarter number.
No QUARTER() in DAX?
Yes, that is correct.
Really?
Yes, it's crazy and doesn't make any sense. Fortunately the workaround is just dividing the month by 3.
Solution:
VAR quarterNumber = CEILING(MONTH([Date])/3,1)
Alternate Solutions:
Since DAX has multiple ways to round numbers, these will also work:
VAR quarterNumber = ISO.CEILING(MONTH([Date])/3,1)
VAR quarterNumber = ROUNDUP(MONTH([Date])/3,0)
Which Solution is Best:
For the values used in my examples, the results will be identical.
For other examples there can be small and subtle differences in the result depending on standards or the type of CPU being used.
ROUNDUP is probably more intuitive to Excel people.
CEILING is probably more intuitive to math people.
ISO.CEILING is ugly to look at in code, personal opinion.
It's not documented but this works:
INT(FORMAT([Date], "q"))
DAX now has quarters! This is some date data:
And these are how you get the quarters and quarter numbers:
The results of these are below:
I think they are assuming you'd create a date dimension in which your Quarter is pre-defined, including the Financial Year. I live in Australia where the Financial Year ends in June, and I've always pre defined the quarters as an added column to the table. IF you're using Power BI/Power Query you can add a query in the M code level (at the import stage).
You can also use this command:
=FORMAT(Date[Date],"q")
And combined them to create things like this
="Q." & FORMAT(Date[Date],"q") & " - "&Date[Year] = e.g. Q.1 - 2021
Add column for get quarter :
Quarter = summary_bu_USD[Start Period].[Quarter]
Add column again and group year in column:
QuarterYear = YEAR(summary_bu_USD[Start Period])&" "&"Q"&RIGHT(summary_bu_USD[Quarter],1)
enter image description here
PostgreSQL provides the date format datatype to store dates. The problem with these dates is however they can't - as far as I know - reason about uncertainty.
Sometimes one does not know the full date of something, but knows it happened in January 1995 or in "1999 or 2000" (date2). There can be several reasons for that:
People don't remember the exact date;
The exact date is fundamentally unknown: for instance a person was last seen on some day and found death a few days later; or
We deal with future events so there is still some chance something goes wrong.
I was wondering if there is a datatype to store such "dates" and how they are handed. It would result in thee-valued logic for some operations like for instance date2 < 20001/01/01 should be true, date2 < 2000/01/01 be possible and date2 < 1998/01/01 should be false.
If no such datatype is available, what are good practices to construct such "table" onself?
There are several different ways to approach fuzzy dates. In PostgreSQL, you can use
a pair of date columns (earliest_possible_date, latest_possible_date),
a date column and a precision column ('2012-01-01', 'year'), or
a range data type (daterange), or
a varchar ('2013-01-2?', '2013-??-05'), or
another table or tables with any of those data types.
The range data type is peculiar to recent versions of PostgreSQL. You can use the others in any SQL dbms.
The kind of fuzziness you need is application-dependent. How you query fuzzy dates depends on which data type or structure you pick. You need a firm grasp on what kinds of fuzziness you need to store, and on the kind of questions your users need answered. And you need to test to make sure your database can answer their questions.
For example, in legal systems dates might be remembered poorly or defaced. Someone might say "It was some Thursday in January 2014. I know it was a Thursday, because it was trash pick-up day", or "It was the first week in either June or July last year". To record that kind of fuzziness, you need another table.
Or a postmark might be marred so that you can read only "14, 2014". You know it was postmarked on the 14th, but you don't know which month. Again, you need another table.
Some (all?) of these won't give you three-valued logic unless you jump through some hoops. ("Possible" isn't a valid Boolean value.)
To add to what Mike posted I would use date comments such as:
date Comment
-------------------------------------------------------------------
1/1/2010 Sometime in 2010
7/8/2014 Customer says they will pay the second week in July
1/1/2015 Package will arrive sometime next year in January
Also, you can use date parts. Create a separate column for the Year, Month, and Day. What ever in unknown leave it blank.
Using Oracle Discoverer 11g
I'm trying to create a report that would generate certain dates. For example
The column would be called Date and the only results I would like to come back are from the current date minus 20 months (assuming 600days). So 20 months back until the current date. Is this possible?\
So far, I am here and can't figure out how to write the 20 months back:
units.Date(SYSDATE, 'DD-Mon-YY - ???)
I was placing this under Tools > Edit Calculations
Let me know if you guys need any more information.
Update: I went ahead and tried this, but recieved this error. Not sure what i means.
It sounds like from your post that you want the time between today and the unit.date time. Meaning, that if your unit.date was 1/1/2015 and today is 4/14/2015, then your calculation would return "103" days. Assuming my interpretation is correct, then you should just enter the following as your calc:
TRUNC(SYSDATE) - TRUNC(units.date)
The EUL_DATE_TRUNC will format the date to a specified format, which I don't think is needed for your calculation.