RethinkDB Filter for range of dates and times - date

My database table has the timestamp format "2019-12-08T13:03:16.502639-0600". I am having difficult figuring out how to filter a 24 hour range of dates so I only get the data plucked from inside that 24 hour running period of time. I need something like now() then back 24 hours on a running basis. I have tried several ways but none of them work.
Do I need to parse the timestamp first and only use the parts I need?

In Python, the query could look like this:
r.table('MyDB').filter(r.row['timestamp'] > r.now() - 24*60*60).run(con)

Related

How can I make a database structure in mongodb for saving the date range?

Here I'm saving the date range using golang. Suppose we have to save the all monday comes between the range of the 1-may-2018 to 14-july-2018.
How we will find all the monday between these range using golang and on the other hand we have set the start_time (8:00 A.M.) and the end_time (6:00 P.M.) of the first two coming monday in the database but on the third monday we have a change in the schedule that there is a time change like start_time (9:00 A.M.) and end_time (5:00 P.M.). Then how I will make my database to make this situation in practically using the golang.
Can Anybody help me for this to solve this solution. I made a database for and I do ppr work on it and make some fields shown below:-
Fields for Schedule //Schedule is a collection name
Id (int)
Day (string)
Start_hours (int)
Start_minutes (int)
End_hours (int)
End_minutes (int)
Start_date (timestamp)
End_date (timestamp)
How I will select monday between the selected range and how will I do the situation I explained above can anybody give guidance to me to make this situation easier. Thank you if this is a basic question then I'm really sorry.
I'd make something like this.
Find the first Monday date from the date range (see for example How do I get the first Monday of a given month in Go?
Mondays happen every week, so you can easily find the rest of dates by adding 7 days till the end date
Store all the Monday dates you found with the start and end times
I wouldn't bother with hours and minutes as you can easily get them from the timestamps in Go. Here is the simplified DB structure I would make
Fields for Schedule //Schedule is a collection name
Id (int)
Day (string)
Date (timestamp) // the actual date
Start (timestamp)
End (timestamp)
You don't need any more fields. You can get the day of the week (Day (string) in your structure, e.g. Monday) from the Date field too, but I believe if you want to query the collection by different days, this might speed things up, but be careful if you need to adjust for time zones. If you work with more than one, then store everything in UTC and you may have an extra filed Timezone, cos a date could be Monday for one zone and Sunday for another.
So, the Schedule will hold weekdays and start and end times for each of them. I'm not sure if you need to store initial date ranges, the Schedule collection will hold that range as well, form the first record to the last one. In my mind, I'd initially populate the collection with a given date range, then later on, I can modify it by adding new days, or deleting them.
When you query this collection with some start and end date range for the Date field, if your first result comes newer than 7 days from the start, this means you miss 1 or more entries from the start. If the last result comes older than 7 days from the range end, this means you miss some entries prior to the range end.
There is nothing specific to Go, in my opinion, Go works well with dates and you don't need any special date structures in your DB.

How can I Add Days to a date property in Cypher?

I have been working on a readmission issue using a clinical graph dataset. Let's say a patient is being readmitted within 30 days. So, this means I need add 30 days to the first date (visit date) for the second visit date.
Here is the Cypher query:
MATCH(p:Person)-[r:PATIENT_HAS]->(e:Encounter)
WITH p,e
MATCH (p)-[r:PATIENT_HAS]-(e2:Encounter) WHERE e2.ADMIT_DATE < (e.ADMIT_DATE + 30)
This query won't work because the date property is in YYYYMMDD format. For example, if it is 20151225, it will give 20151255. But I need to get it as 20160124 after adding 30 days. Is there any other way to use a different format than YYYYMMDD. I know that there is string format as YYYY-MM-DD, but what is the way to use this format for adding days?
How can accomplish this?
I would appreciate your help.
The best way to deal with dates in Neo4j in my opinion is to have them saved as UNIX epoch time in miliseconds or seconds. We have a great plugin in Neo4j called apoc procedures that allows you to use awesome procedures. In your specific case I would utilize apoc.date.*procedures and parse your date format to epoch time in seconds.
MATCH (e:Encounter)
WITH e,apoc.date.parse(e.ADMIT_DATE,"s","YYYYMMDD") as unix
set e.unix = unix
So now your query would look like :
MATCH(p:Person)-[r:PATIENT_HAS]->(e:Encounter)
MATCH (p)-[r:PATIENT_HAS]-(e2:Encounter)
WHERE e2.unix < e.unix + (30 * 24 * 60 * 60)
Ofcourse you can simplify this query and make it shorter:
MATCH (e2:Encounter)-[r:PATIENT_HAS]-(p:Person)-[r:PATIENT_HAS]->(e:Encounter)
WHERE e2.unix < e.unix + (30 * 24 * 60 * 60)
You can construct a Duration, and add it directly to a date.
Here is an example in Cypher:
RETURN Date() + Duration({days: 30})
Reference: https://neo4j.com/docs/cypher-manual/current/functions/temporal/duration/
I'm currently using Neo4j 4.0.3 and the following works:
RETURN REPLACE(toString(date('20151225') + duration('P30D')),'-','')
// "20160124"
We found out a temporary solution: We are setting a specific date in the past (1/1/2009 in our case), and are getting the difference between the first visit (e.ADMIT_DATE-1/1/2009) and the second visit (e2.ADMIT_DATE-1/1/2009) as days. Then we are able to get the difference between the first and second visits (e2.DAYS-e.DAYS) to figure out 30 days for the readmission criteria.
I had checked out the apoc procedures plugin and it seemed a bit complicated. But I can definitely retry it. Many thanks for your great responses folks.

Time between two timestamp (postgresql)

Is there a way to have the time (HH:mm) between two timestamps in postgresql ?
The difference between two timestamps can be calculated using -, e.g.:
timestamp_one - timestamp_two
However, the result is not a "time", but an "interval".
You can display that in the form HH:mm using to_char():
to_char(timestamp_one - timestamp_two, 'HH:mi')
but what do you expect for differences larger then 24 hours?

Postgres - Convert Date Range to Individual Month

I have found similar help, but the issue was more complex, I am only good with the basics of SQL and am striking out here. I get a handful of columns a,b,c,startdate,enddate and i need to parse that data out into multiple rows depending on how many months are within the range.
Eg: a,b,c,1/1/2015, 3/15,2015 would become:
a,b,c,1/1/2015,value_here_doesnt_matter
a,b,c,2/1/2015,value_here_doesnt_matter
a,b,c,3/1/2015,value_here_doesnt_matter
Does not matter if the start date or end date is on a specific day, the only thing that matters is month and year. So if the range included any day in a given month, I'd want to output start days for each month in the range, with the 1st as a default day.
Could I have any advice on which direction to begin? I'm attempting generate_series, but am unsure if this is the right approach or how to make it work with keeping the data in the first few arbitrary columns consistent.
I think generate_series is the way to go. Without knowing what the rest of your data looks like, I would start with something like this:
select
a, b, c, generate_series(startdate, enddate, interval '1 month')::date
from
my_table

After midnight times in postgresql

I have data from a text file I'm reading into a postgres 9.1 table, and the data looks like this:
451,22:30:00,22:30:00,San Jose,1
451,22:35:00,22:35:00,Santa Clara,2
451,22:40:00,22:40:00,Lawrence,3
451,22:44:00,22:44:00,Sunnyvale,4
451,22:49:00,22:49:00,Mountain View,5
451,22:53:00,22:53:00,San Antonio,6
451,22:57:00,22:57:00,California Ave,7
451,23:01:00,23:01:00,Palo Alto,8
451,23:04:00,23:04:00,Menlo Park,9
451,23:07:00,23:07:00,Atherton,10
451,23:11:00,23:11:00,Redwood City,11
451,23:15:00,23:15:00,San Carlos,12
451,23:18:00,23:18:00,Belmont,13
451,23:21:00,23:21:00,Hillsdale,14
451,23:24:00,23:24:00,Hayward Park,15
451,23:27:00,23:27:00,San Mateo,16
451,23:30:00,23:30:00,Burlingame,17
451,23:33:00,23:33:00,Broadway,18
451,23:38:00,23:38:00,Millbrae,19
451,23:42:00,23:42:00,San Bruno,20
451,23:47:00,23:47:00,So. San Francisco,21
451,23:53:00,23:53:00,Bayshore,22
451,23:58:00,23:58:00,22nd Street,23
451,24:06:00,24:06:00,San Francisco,24
It is from a timetable for a commuter rail line, Caltrain. I'm trying to query stations, to get train arrival and departure times. I did this several months ago in MySql, and I got
select * from trains as a, trains as b where a.trip_id=b.trip_id and a.st
op_id='San Antonio' and b.stop_id='San Carlos' and a.arrival_time < b.arrival_ti
me;
So far so good, pretty straightforward. However, when I tried copying the data into a postgres database, I got an error for the various columns that had times after midnight, either 24 or 25:00:00 something. However, if I change them to be 00:00:00 and 01:00:00 something, won't that mess with the query? A time after midnight will appear to be before the starting time? MySql apparently didn't have a problem with those times, and I'm not sure what to do. I'm thinking I should use the last column, or maybe convert the times to something that doesn't take into account PM/AM?
You should try using the interval type for the time columns. Those will keep track of the number of hours, minutes, and seconds instead of trying to record a time of day.
See the PostgreSQL documentation on dates and times.
An interval can have a time component greater than 24 hours, unlike the time datatype that is confined to 00:00 <= x <= 23:59.