Need to split out whole and partial hours from a time period (postgresql) - postgresql

I have a table of data that data that contains a date, time-in, and time-out pulled from a postgresql db table. Currently the table shows the time a person checked in, and out over a period of hours. I need to use postgresql to split out the hours between time-in and time-out such that I capture each full hour (eg. 10am-11am) and each fraction (eg. 4.15pm to 5pm) of the hour during which that person was checked in. So if a person checked in at 9.30am, took a 1 hour lunch break at 12, and checked out at 4.30pm, my current table would show two rows for that member i.e. one row for the time before lunch break, and the time after the lunch break. I want to show each hour (whole or partial) on each row, with 1 representing that 1 whole hour, and the minute portion to capture the partial hour worked.
Below are the before and after images. Any help is appreciated.
Shows that I want to convert from
Shows that I want to convert to
Thanks

try this.
CAST(time_out - time_in AS TIME)

Related

159h vs 160h breaks override relative time in time range of table in Grafana

I have scrape data in Prometheus every hour around the 43 minute mark for the past around 5 days. I would like to display the hourly scrape events from the past week in a table in Grafana. In the table, when I set the Override relative time option in Time range to 6 days, the table properly displays all my hourly data, since I only have 5 days of data. However, when I set the override to 7 days, it only shows me data from the first day, and not even all the data from that day.
I tried narrowing the override down, and found that the threshold is 159h vs 160h. When it's 159h, the table works as expected, but when it's 160h, suddenly the table breaks. Why does increasing the time range by 1 hour cause the table to break, especially considering no additional data is even being considered?

Repeat each date specific number of times

I have gotten so much help form this forum from times to times, but this is the first time I am posting a question.
What I need is:
I have an excel file that is extracted from a machine, which records temperatures every 15 minutes per hour, 24 hours per day, every day per month... (96 rows per day)
In Column A we have the dates
01/09/2017 (x96 times)
02/09/2017 (x96 times)
...
...
...
30/09/2017 (x96 times)
Sometimes this machine gets stuck and stops recording, so until it gets back to work and start recording again, I have already missed some records, which I have to enter by hand.
So, I need a macro, to check if each date of the month has 96 rows in column A and if not then add the missing rows... It happens some dates to have 70 rows, so in that case I need the macro to insert another 26 rows of the same date...
I don't know if this is easy, but it would be very helpful if someone could give me a solution.

Counting the number of times a specific hour appears in a column after extraction in postgresql

after using the extract function to extract an hour value from a timestamp in postgresql, how can I count the number of times each value appears? I'm trying to produce the number of calls coming into a helpdesk based on time of day. So I need to be able to count the number that came in at 2pm, 3pm, 4pm and so on. Thanks for any help.

PostgreSQL - Have one serial type column but reset every day (unique combination with other date type column)

i need to have this table that will have a serial type column in my PostgreSQL database that will reset every day and will be unique combination with other date type column.
For example today i insert 2 rows
SerialId, Date
1, '08.12.2016'
2, '08.12.2016'
But tomorrow the next insert should be with SerialId = 1 and tomorrows date
1, '09.12.2016' ...
The problem is that not only one users makes inserts in this table and i can't have some global variable in my application that will count and reset every day.
Any ideas?
Thanks in advance.
I would just let the sequence keep running, but if you really want to reset it, you could define a cron job that runs at midnight and issues
ALTER SEQUENCE ... RESET;
If the application is really busy around midnight, you have a race condition there, because there is no guarantee that the sequence will be reset precisely at midnight, but if there is not much traffic at this time you might get away with it.

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.