Algolia sort by date - algolia

I know that I can use a unix timestamp to filter dates eg: date>=1362873600,date<=1366416000
But is it possible to sort dates using asc/desc? I would like to sort my result by newest/oldest item in a falling order

Yes you can replicate an index and set its custom ranking to use the date attribute (desc or asc).
https://www.algolia.com/doc/guides/managing-results/refine-results/sorting/

Related

Difference between previous and current month

I'm trying to calculate the difference between current month and previous month.
The date field in my DB called C_DATE (dd/MM/YYYY), and I'm using in my sheet a date filter in the format: Year(C_DATE)&'-'& Num( Month(C_DATE),'00'). So when the user choose month (for example 2022-05), he will see the difference in the data between May and April.
I've already tried to calculate by:
count ({<Answer_num={">=6"}, C_DATE={">=$(=(MonthStart(Max(C_DATE))))<=$(=(MonthEnd(Max(C_DATE))))"}>}Answer_num) -
count ({<Answer_num={">=6"}, C_DATE={">=$(=(AddMonths( MonthStart(Max(C_DATE)),-1)))<=$(=(MonthStart(Max(C_DATE)))))"}>}Answer_num)
but i'm getting wrong outcome. is it maybe because of the date filter format? What can I do?
Thank you!
I think this is a matter of getting the date format correct and using the proper set operator. This may work:
=Count({1<Answer_num={">=6"}, C_DATE={">=$(=Date(MonthStart(Max(C_DATE)), 'DD/MM/YYYY'))<=$(=Date(MonthEnd(Max(C_DATE)), 'DD/MM/YYYY'))"}>*$} Answer_num)
-
Count({1<Answer_num={">=6"}, C_DATE={">=$(=Date(AddMonths(MonthStart(Max(C_DATE)),-1), 'DD/MM/YYYY'))<=$(=Date(MonthStart(Max(C_DATE)), 'DD/MM/YYYY'))"}>*$} Answer_num)
This adds the =Date(..., 'DD/MM/YYYY') expression to the set expressions so that the formatting matches the format of the [C_DATE] field. We also add the * set operator so that we get the intersection between the selection ($) and our ">=...<=..." set expression.
I'm sort of guess on the use of the intersection operator, it could be that the union operator (+) is the correct one to use in this case.

Searching for max date dynamically

I have the need to compare a date column with the max(date column) while making a filter selection.
E.g., when I compare [Date] = {max([Date])}, it finds the max/latest date in the entire data and compares. This gives me correct result when the latest month is included in the filter, but fails if I keep all months except the latest.
Is there a way in which the latest date can be searched in the subset of the data (based on filter selection)?
I am working with Redshift database (live connection).
look at the attached. https://www.dropbox.com/s/5zdkw9n003rxgvl/170524%20stack%20question.twbx?dl=0
{fixed : max(date)} will reflect only what is in the context filter.

Is ISO8601 the best date-format for PostgreSQL jsonb when i want to filter by the date?

I'm new to PostgreSQL and I have the following question:
I have a table with just an id-column and a data-column, which uses the jsonb-type. Inside the jsonb-object I have a datetime field. I read in various posts, that I should use the ISO-8601 dateformat to store in the DB.
I want to filter my table by date like this:
SELECT * FROM table WHERE data->'date' > '2016-01-01T00:00'
Is this really the best date-format for this purpose?
Thanks in advance :)
IMHO Your query should produce
ERROR: operator does not exist: jsonb > timestamp with time zone
If I get it right. In case you change -> to ->> it should get a text value instead of jsonb field (which is also not comparable to timestamp).
It should be smth like
SELECT * FROM table WHERE (data->>'date')::timestamptz > '2016-01-01T00:00' to work
The big advantage of that format is that string order corresponds to date order, so a comparison like the one you quote in your question would actually work as intended.
A second advantage is that a timestamp in that format can easily be converted to a PostgreSQL timestamp with time zone value, because the type input function understands this format.
I hope you are not dealing with dates “before Christ”, because it wouldn't work so easily with those.

Group by dates range mongodb

I've got this collection in the db
[{name:1,startDate:1/1/13,endDate:2/2/13,number:10},
{name:1,startDate:2/2/13,endDate:5/2/13,number:15},
{name:2,startDate:2/1/13,endDate:5/2/13,number:25},
{name:1,startDate:5/2/13,number:17},
{name:2,startDate:5/2/13,number:30}]
I want to get a list of start dates with the average of the number within the range of this start date, for example, the result of this collection would be:
{date:1/1/13,avg:10},
{date:2/1/13,avg:17.5},
{date:2/2/13,avg:20},
{date:5/2/13,avg:23.5}
I've tried some map-reduce and group but none can group by start date properly,
please help.
You can do this by using the aggregation pipleline:
db.collectionName.aggregate([{$group:{_id:'$startDate',avg:{$avg:'$number'}}}])
This will group the collection by startDate and with the aggregation being the average of the number.
If you strictly want the _id field labelled as startDate, you use the $project operation in the pipeline:
db.collectionName.aggregate([{$group:{_id:'$startDate', avg:{$avg:'$number'}}},{$project:{'startDate':'$_id','avg':'$avg',_id:0}}])
This will give the desired result.
For lack of a better way i just did it from the server side,
first getting all the startDate s and then preforming an iteration that finds every document in the date range and averaging it's number...
Not quit how i expected to solve this, but it works properly.

Approximate date column

One of my customers would like to have a custom date column, where he could store the year only, a combination of month and year (without the day), or a classic date with day, month and year.
It should be possible to use this field for sorting the data. A "month-year" date should be considered as "01-month-year" for the sort, and a "year" date should be treated as "01-01-year" for the sort.
I could imagine two solutions to that:
Store the date in the standard "day-month-year" format, and keep in a separate column how the date was entered ("year", "month-year", "day-month-year"), so the approximate date can be displayed exactly how it was entered.
Use some sort of custom date column in the postgresql database.
Has anyone experience with that?
You could use date-time functions to extract date components. I don't think it has any sense to create additional columns. Also, some databases allow to create indexes by functions.