sql - How to generate a load report in postgresql when data is logged as sessions? -


i have database contains sessions: like:

user, date_first, date_last john, 2015-01-01, 2015-08-23 john, 2015-07-02, 2016-09-01 mary, 2015-01-01, 2015-01-03 

i want generate report tells me how many users logged-in per day/week/month.

is possible via sql in postgresql?

expected output monthly report:

month,   users 2015-01,  25 2015-02,  50 2015-03,  12 

it's possible using series generating function generate_series(start, stop, step interval). example months report:

select to_char(g.d, 'yyyy-mm') month, count(*) users generate_series(        (select date_trunc('month', min(date_first)) sessions),        (select max(date_last) sessions), '1 month') g(d) inner join sessions s   on (g.d, g.d + interval '1 month' - interval '1 day')     overlaps (s.date_first, s.date_last) group 1 having count(*) > 0 order 1 

this sample on sql fiddle.

similarly, can write queries days , weeks reports.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -