Add all timestamp data according to date - postgresql

I have this access_log table
create_time | time_spent
--------------+------------
NOV 02, 2017 | 00:00:00
NOV 02, 2017 | 01:45:04
NOV 02, 2017 | 00:00:00
NOV 02, 2017 | 00:00:00
NOV 02, 2017 | 00:39:29
NOV 02, 2017 | 03:05:49
NOV 03, 2017 | 03:58:57
NOV 03, 2017 | 00:52:29
NOV 03, 2017 | 02:53:25
using this table, make looks like this using PostgreSQL. it should add all time_spent column data of respective date
create_time | time_spent
--------------+------------
NOV 02, 2017 | 05:30:22
NOV 03, 2017 | 07:44:51
Thank you in advance.

a simple group by here:
t=# create table access_log (create_time date,time_spent time);
CREATE TABLE
t=# copy access_log from stdin delimiter '|';
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself, or an EOF signal.
>> NOV 02, 2017 | 00:00:00
NOV 02, 2017 | 01:45:04
NOV 02, 2017 | 00:00:00
NOV 02, 2017 | 00:00:00
NOV 02, 2017 | 00:39:29
NOV 02, 2017 | 03:05:49
NOV 03, 2017 | 03:58:57
NOV 03, 2017 | 00:52:29
NOV 03, 2017 | 02:53:25>> >> >> >> >> >> >> >>
>> \.
t=# select sum(time_spent), create_time from access_log group by create_time;
sum | create_time
----------+-------------
05:30:22 | 2017-11-02
07:44:51 | 2017-11-03
(2 rows)

Related

Working with date ranges - creating a flag in panel

I have two datasets:
Panel - years from 2010 to 2020
Ranges for each firm (one or many) when they took a loan - also the range indicates the loan duration
First can look like this (each company has all the year-observation records):
id year
1 2010
1 ...
1 2020
2 2010
2 ...
2 2020
Second can look like this (so all variety, a company can have all years with a loan, some gaps in the beginning and in the end):
id start end
1 2010 2011
1 2011 2014
1 2017 2018
1 2012 2020
2 2014 2018
3 2011 2012
3 2015 2018
3 2018 2020
4 2011 2012
4 2015 2018
4 2010 2018
The idea is to merge the two, so each company gets 0 or 1 for a year, 1 if there was a loan that year (so any of the ranges matching), and 0 if a company didn't have a loan that year.
Example company based on the above would look like this:
id year flag
3 2010 0
3 2011 1
3 2012 1
3 2013 0
3 2014 0
3 2015 1
3 2016 1
3 2017 1
3 2018 1
3 2019 1
3 2020 1
Hope that makes sense. I tried inrange() but there are too many different scenarios and my code gets messy, thought there is a simple and clean way to do it.
If you work on the second dataset, you can get something fit to merge with your main dataset.
clear
input id start end
1 2010 2011
1 2011 2014
1 2017 2018
1 2012 2020
2 2014 2018
3 2011 2012
3 2015 2018
3 2018 2020
4 2011 2012
4 2015 2018
end
gen long ID = _n
gen toexpand = end - start + 1
expand toexpand
bysort ID : gen year = start + _n - 1
drop start end ID toexpand
duplicates drop id year, force
sort id year
list, sepby(id)
+-----------+
| id year |
|-----------|
1. | 1 2010 |
2. | 1 2011 |
3. | 1 2012 |
4. | 1 2013 |
5. | 1 2014 |
6. | 1 2015 |
7. | 1 2016 |
8. | 1 2017 |
9. | 1 2018 |
10. | 1 2019 |
11. | 1 2020 |
|-----------|
12. | 2 2014 |
13. | 2 2015 |
14. | 2 2016 |
15. | 2 2017 |
16. | 2 2018 |
|-----------|
17. | 3 2011 |
18. | 3 2012 |
19. | 3 2015 |
20. | 3 2016 |
21. | 3 2017 |
22. | 3 2018 |
23. | 3 2019 |
24. | 3 2020 |
|-----------|
25. | 4 2011 |
26. | 4 2012 |
27. | 4 2015 |
28. | 4 2016 |
29. | 4 2017 |
30. | 4 2018 |
+-----------+
After the merge 1:1 id year it is
gen flag = _merge == 3
```

Unable to Calculate 7 Day Moving Average due to inconsistent dates

I just noticed that my code below is not actually a 7 day moving avg, and instead it is a 7 row moving avg. The dates in my table spans several months and I am trying to iron out since I have inconsistent data flow so I can't expect the last 7 rows of the window function to actually represent a 7 day avg. Thanks.
select date, sales,
avg(sales) over(order by date rows between 6 preceding and current row)
from sales_info
order by date
You can get a bit closer to a true 7 day moving average by using RANGE instead of ROWS for your range specification.
Read more about window function frames here.
I believe this should work for you:
select date, sales,
avg(sales) over(order by date range between '6 days' preceding and current row)
from sales_info
order by date;
Here's a demonstration with made up data:
SELECT i,
t,
avg(i) OVER (ORDER BY t RANGE BETWEEN '6 days' preceding and current row) FROM (
SELECT i, t
FROM generate_series('2021-01-01'::timestamp, '2021-02-01'::timestamp, '1 day') WITH ORDINALITY as g(t, i)
) sub;
i | t | avg
----+---------------------+------------------------
1 | 2021-01-01 00:00:00 | 1.00000000000000000000
2 | 2021-01-02 00:00:00 | 1.5000000000000000
3 | 2021-01-03 00:00:00 | 2.0000000000000000
4 | 2021-01-04 00:00:00 | 2.5000000000000000
5 | 2021-01-05 00:00:00 | 3.0000000000000000
6 | 2021-01-06 00:00:00 | 3.5000000000000000
7 | 2021-01-07 00:00:00 | 4.0000000000000000
8 | 2021-01-08 00:00:00 | 5.0000000000000000
9 | 2021-01-09 00:00:00 | 6.0000000000000000
10 | 2021-01-10 00:00:00 | 7.0000000000000000
11 | 2021-01-11 00:00:00 | 8.0000000000000000
12 | 2021-01-12 00:00:00 | 9.0000000000000000
13 | 2021-01-13 00:00:00 | 10.0000000000000000
14 | 2021-01-14 00:00:00 | 11.0000000000000000
15 | 2021-01-15 00:00:00 | 12.0000000000000000
16 | 2021-01-16 00:00:00 | 13.0000000000000000
17 | 2021-01-17 00:00:00 | 14.0000000000000000
18 | 2021-01-18 00:00:00 | 15.0000000000000000
19 | 2021-01-19 00:00:00 | 16.0000000000000000
20 | 2021-01-20 00:00:00 | 17.0000000000000000
21 | 2021-01-21 00:00:00 | 18.0000000000000000
22 | 2021-01-22 00:00:00 | 19.0000000000000000
23 | 2021-01-23 00:00:00 | 20.0000000000000000
24 | 2021-01-24 00:00:00 | 21.0000000000000000
25 | 2021-01-25 00:00:00 | 22.0000000000000000
26 | 2021-01-26 00:00:00 | 23.0000000000000000
27 | 2021-01-27 00:00:00 | 24.0000000000000000
28 | 2021-01-28 00:00:00 | 25.0000000000000000
29 | 2021-01-29 00:00:00 | 26.0000000000000000
30 | 2021-01-30 00:00:00 | 27.0000000000000000
31 | 2021-01-31 00:00:00 | 28.0000000000000000
32 | 2021-02-01 00:00:00 | 29.0000000000000000

Finding the end date from asynchronous starting but consecutive running date ranges

I need to help with a database model and and efficient calculation method of the last end date.
The ranges have a start date but will wait to start until the currently active ranges have ended. Changing the length or start of one range should cascade to the waiting ranges and push them back. But they can never start before their start date.
The database mock here is just to explain the problem and doesn't need to be followed as long as it works properly.
| id | start | length | 01 02 03 04 05 06 07 08 09 10 11 12 |
| 1 | 03 | 2 | ------ |
| 2 | 06 | 2 | ------ |
I am aware that I can brute force the answer to get the last date. But I'm looking for a more efficient solution as there are many ranges and the end date needs to read often.
Also is this something that's possible to calculate in a PostgreSQL query? Or maybe keep end date fields up to date with triggers?
Examples
Here range 2 starts on 04 but can't be used until after range 1 finishes.
| id | start | length | 01 02 03 04 05 06 07 08 09 10 11 12 |
| 1 | 02 | 5 | ------------------ |
| 2 | 04 | 5 | ------------------ |
If we update the range 1, range 2 should move up with it.
| id | start | length | 01 02 03 04 05 06 07 08 09 10 11 12 |
| 1 | 02 | 4 | -------------- |
| 2 | 04 | 5 | ------------------ |
But range 2 shouldn't move past its start date.
| id | start | length | 01 02 03 04 05 06 07 08 09 10 11 12 |
| 1 | 02 | 1 | -- |
| 2 | 04 | 5 | ------------------ |
If you just take the latest start date and add its duration you will get the wrong answer.
| id | start | length | 01 02 03 04 05 06 07 08 09 10 11 12 |
| 1 | 04 | 5 | ------------------ |
| 2 | 05 | 2 | ------ |
Get: 7
Expected: 10

In Postresql. How to replicate rows based on the number value in a column

Here is problem below:
Name Start Time End Time Number
A1 5:13 PM 5:43 PM 0
A2 7:06 PM 8:51 PM 2
A3 6:36 PM 8:06 PM 3
A4 4:51 PM 7:51 PM 4
I would like to replicate rows based on Number values and include three new Columns (New_Start_Time, New_End_Time, and Minutes) I'm new to Sql, how I can do this in Postresql?
I expected the result below:
Name Start Time End Time Number New_Start_Time New_End_Time
A1 5:13 PM 5:43 PM 0 5:13 PM 5:43 PM
A2 7:06 PM 8:51 PM 2 7:06 PM 8:00 PM
A2 7:06 PM 8:51 PM 2 8:00 PM 8:51 PM
A3 6:36 PM 8:06 PM 3 6:36 PM 7:00 PM
A3 6:36 PM 8:06 PM 3 7:00 PM 8:00 PM
A3 6:36 PM 8:06 PM 3 8:00 PM 8:06 PM
A4 4:51 PM 7:51 PM 4 4:51 PM 5:00 PM
A4 4:51 PM 7:51 PM 4 5:00 PM 6:00 PM
A4 4:51 PM 7:51 PM 4 6:00 PM 7:00 PM
A4 4:51 PM 7:51 PM 4 7:00 PM 7:51 PM
This can be done using generate_series() and calculating the number of hours between start and end time.
So we need to first calculate the "base start time" by rounding the start_time to the full hour. This is also used to add the hours when duplicating the rows:
with rounded as (
select name,
start_time,
end_time,
date_trunc('hour', start_time)::time as base_start_time,
extract(hour from (date_trunc('hour', end_time) + interval '1 hour') - date_trunc('hour', start_time))::int as num_hours
from times
)
select name,
start_time,
end_time,
case
when h = 1 then start_time
else base_start_time + interval '1 hour' * (h - 1)
end as new_start_time,
case
when h = num_hours then end_time
else base_start_time + interval '1 hour' * h
end as new_end_time
from rounded
cross join generate_series(1, num_hours, 1) as t(h)
order by name, new_start_time;
The CTE is used to calculate the base offset and the number of hours that need to be generated. If you are sure you can trust your number column, you can replace the extract(hour ...) as num_hours expression with just number as num_hours.
The new start and new end is then calculated based on which "hour" the row reflects. For the first hour we use the existing start time, for all others we just add the number of hours needed. For the new end time we need to check if it's the last hour.
The above returns:
name | start_time | end_time | new_start_time | new_end_time
-----+------------+----------+----------------+-------------
A1 | 17:13 | 17:43 | 17:13 | 17:43
A2 | 19:06 | 20:51 | 19:06 | 20:00
A2 | 19:06 | 20:51 | 20:00 | 20:51
A3 | 18:36 | 20:06 | 18:36 | 19:00
A3 | 18:36 | 20:06 | 19:00 | 20:00
A3 | 18:36 | 20:06 | 20:00 | 20:06
A4 | 16:51 | 19:51 | 16:51 | 17:00
A4 | 16:51 | 19:51 | 17:00 | 18:00
A4 | 16:51 | 19:51 | 18:00 | 19:00
A4 | 16:51 | 19:51 | 19:00 | 19:51
Online example: https://rextester.com/GAZP30312

Why did my branches completely merge?

I'm new to mercurial. I try to work on 2 versions of my software. I have named 2 branches : one is "v8" (old/stable) other is "default".
I thought I understood things but now I seem to have merged both versions.
This is what it looks like (removed info from before branch) :
o changeset: 39:1e72986020bd
| tag: tip
| parent: 37:406b8e897030
| user: ME <me#example.com>
| date: Thu Jan 16 09:53:55 2014 +0100
| summary: Suppression des DCU
|
| o changeset: 38:7381e3f2309b
|/| branch: v8
| | parent: 31:611a4416e4a5
| | parent: 37:406b8e897030
| | user: ME <me#example.com>
| | date: Wed Jan 15 19:21:58 2014 +0100
| | summary: fixforbrowser sur impressiondecomptes
| |
o | changeset: 37:406b8e897030
| | user: ME <me#example.com>
| | date: Wed Jan 15 19:02:18 2014 +0100
| | summary: fixforbrowser sur impressiondecomptes
| |
o | changeset: 36:d7c0feaad38a
| | user: ME <me#example.com>
| | date: Wed Jan 15 18:57:44 2014 +0100
| | summary: Cosmétique
| |
o | changeset: 35:d2b4c3130b61
| | user: ME <me#example.com>
| | date: Sun Jan 12 14:38:36 2014 +0100
| | summary: Cosmétique encore
| |
o | changeset: 34:5447f904a336
| | user: ME <me#example.com>
| | date: Fri Jan 10 03:51:33 2014 +0100
| | summary: projet
| |
o | changeset: 33:f48c4023d822
| | user: ME <me#example.com>
| | date: Fri Jan 10 03:50:06 2014 +0100
| | summary: cosmétique (beaucoup de fiches)
| |
o | changeset: 32:cc6b2de08004
| | parent: 30:5b4bef6aad09
| | user: ME <me#example.com>
| | date: Fri Jan 10 02:12:59 2014 +0100
| | summary: cosmétique
| |
| # changeset: 31:611a4416e4a5
| | branch: v8
| | parent: 29:0c47053f9a7b
| | user: ME <me#example.com>
| | date: Fri Jan 10 02:11:01 2014 +0100
| | summary: purge deleted ?
| |
o | changeset: 30:5b4bef6aad09
|\| parent: 25:74d793961989
| | parent: 29:0c47053f9a7b
| | user: ME <me#example.com>
| | date: Fri Jan 10 02:09:04 2014 +0100
| | summary: v9 after fixes
| |
| o changeset: 29:0c47053f9a7b
| | branch: v8
| | parent: 27:79855eedf019
| | user: ME <me#example.com>
| | date: Fri Jan 10 01:57:22 2014 +0100
| | summary: 8.084 ok?
| |
+---o changeset: 28:e60a7447adf4
| |/ branch: v8
| | parent: 25:74d793961989
| | parent: 27:79855eedf019
| | user: ME <me#example.com>
| | date: Fri Jan 10 01:41:05 2014 +0100
| | summary: 8.084 ??
| |
| o changeset: 27:79855eedf019
| | branch: v8
| | user: ME <me#example.com>
| | date: Wed Jan 08 16:57:12 2014 +0100
| | summary: fix detection des fichiers de récap sur imports feuillets
| |
| o changeset: 26:19d2f4b2d867
| | branch: v8
| | parent: 23:14219f06bc1d
| | user: ME <me#example.com>
| | date: Wed Jan 08 16:49:35 2014 +0100
| | summary: fix qpdep et qpdrm sur imports feuillets
| |
o | changeset: 25:74d793961989
| | user: ME <me#example.com>
| | date: Wed Jan 08 16:44:30 2014 +0100
| | summary: premieres modifs v9
| |
o | changeset: 24:1af3020ba120
| | parent: 22:7307bc3e87ba
| | user: ME <me#example.com>
| | date: Thu Dec 26 15:47:20 2013 +0100
| | summary: Initial v9
| |
| o changeset: 23:14219f06bc1d
|/ branch: v8
| user: ME <me#example.com>
| date: Thu Dec 26 15:41:55 2013 +0100
| summary: Branche stable v8
|
o changeset: 22:7307bc3e87ba
| user: ME <me#example.com>
| date: Fri Dec 20 18:50:50 2013 +0100
| summary: 8.083
Rev 31 seems to be the last "good" v8
then I did hg update default and worked on "default" branch.
I committed up to rev 36 because I knew I had to make a fix I would want to merge with stable/v8
I did the change, committed (rev 37)
then I switched to v8 with hg update v8
then I tried to merge the change from default with hg merge 406b8e897030
then I committed (rev 38)
I did a last commit after removing 3 binary (DCU) files that were in one of the repository although *.dcu are .hgignored.
Now, when I switch branches, I see no file change. I can go back to rev 31 with hg update v8 (probably useless) and hg update --rev 31 but I would like :
to understand what I did wrong
to get 2 clean branches back
If you know of a clear explanation of how branches and merge work, I'd love to read it.
Branches stores diverged history of changes in sources (of anything)
Merging branches brings to target branch fro source branch all changes, appeared from latest divergence point: it can be branchpoint or latest mergeset's parent
If you have to port only subset of changes from branch to branch (1+ changeset, but < ALL), you have to hg graft only these changesets