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

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.

Related

How do I find the time difference between the first record and the current record

I have a table that selects record every minute. I want to add a column that calculates how much time has passed between my current record and the first record. But I am not sure how to do that.
For example, if my first record has time 2:30, second has time 2:50 and third has time 3:00, then for my new column: time_passed, it will have values 0,20,30.
Thank you in advance!
Assuming your column is of type minute (u/17), you can simply do this:
update time_passed: `int$your_col - first your_col from tbl
This will calculate the underlying number of minutes from first record for each record. If you want to see the result as minutes as well, simply remove the cast to int.

PostgreSQL - extracting date ranges in local time?

CURRENT SITUATION:
I have a table of wildfire incidents with a timestamp with time zone (timestamptz) field to track when the observation occurred.
Everything in the data creation process is in UTC: the incoming data from the source, the app server that inserts the data, the insert python code (appends a "Z" to the time), and the database server are all in UTC.
The incidents' geographic extent spans several time zones in the US, Canada, and Mexico.
PROBLEM:
I've been querying on a day's worth of data in UTC time, but need to extract out data relative to local time. The midnight to midnight range will be different in each time zone.
My use case now is one day, but I was asked to consider arbitrary time ranges. E.g.: find all incidents in the hottest part of the day (say 10:00 to 18:00) local time.
This table is quite large and I have an index on the timestamptz field right now. Any changes I make will need to work with an index.
Account for daylight saving time.
I have a method to get the time zone for each record, so I don't need help with that.
I created a test table for this with a timestamptz field ts_with and a varchar field for the time zone tz. The following query returns what I want, so I feel like I'm making progress.
SELECT
name, test_tz.ts_with, test_tz.tz,
TIMEZONE(test_tz.tz, test_tz.ts_with) as timezone_with
FROM fire_info.test_tz
WHERE TIMEZONE(test_tz.tz, test_tz.ts_with) BETWEEN
'2018-08-07 00:00:00' AND '2018-08-07 23:59:59';
QUESTIONS:
Will this use my index? I'm thinking the timezone function will avoid it. Any solution for that? I'm considering adding another condition to the where clause that selects on timestamptz buffered by a day on either side. That would use the index and then the timezone function isn't sorting through too much data (~6k records per day during fire season). Would PG figure that out?
The timezone function is giving me DST offsets (e.g.: Denver is currently UTC-06). I assume I'll get standard time after DST ends. If I run a query in December for data in August, will it apply standard time or DST?
thanks!!!
The way you wrote the query, it cannot use an index on ts_with.
To use an index, the condition would have to be of the form ts_with <operator> <constant>, and there is no way to rewrite the query in that fashion.
So you should create a second index on timezone(test_tz.tz, test_tz.ts_with).

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.

Need to split out whole and partial hours from a time period (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)

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.