Count data per day in a specific month in Postgresql -
i have table create date called created_at
, delete date called delete_at
each record. if record deleted, field save date; it's logic delete.
i need count active records
in specific month. understand active record
me, let's see example:
for example we'll use hypothetical record:
id | created_at | deleted_at 1 | 23-01-2014 | 05-06-2014
this record active every days between creation date , delete date. including last. if need count active record march, in case, record must counted in every days of month.
i have query (really easy do) show actives records specific month, principal problem how count actives each day in month.
select date_trunc('day', created_at) dia_creacion, date_trunc('day', deleted_at) dia_eliminacion mytable created_at < to_date('01-04-2014', 'dd-mm-yyyy') , (deleted_at null or deleted_at >= to_date('01-03-2014', 'dd-mm-yyyy'))
here are:
select to_date('01-03-2014', 'dd-mm-yyyy') + g.i, count( case (to_date('01-03-2014', 'dd-mm-yyyy') + g.i) between created_at , coalesce(deleted_at, to_date('01-03-2014', 'dd-mm-yyyy') + g.i) when true 1 else null end) generate_series(0, to_date('01-04-2014', 'dd-mm-yyyy') - to_date('01-03-2014', 'dd-mm-yyyy')) g(i) left join mytable on true group 1 order 1;
you can add more specific condition joining relevant records mytable
, without gives idea how achieve counting desired.
Comments
Post a Comment