PostgreSQL: Row count over time -
i have simple mysql sql-script, outputs me row count on time specific table (based on datetime field in table)
select concat('m-', s.label) , s.cnt , @tot := @tot + s.cnt running_subtotal ( select date_format(t.created,'%y-%m') `label` , count(t.id) cnt `templates` t group `label` order `label` ) s cross join ( select @tot := 0 )
now want migrate postgresql, have no idea how migrate variables pg-based syntax.
the inner statement is, of course, no problem:
select to_char(t.created,'yyyy-mm') label , count(t.id) cnt templates t group label order label
anyone here, can me variable-part?
here's simple table data:
create table "templates" ( "id" bigserial, "title" varchar(2048) default null::character varying, "created" timestamp, primary key ("id") ); insert templates(title, created) values('test', '2011-03-01'); insert templates(title, created) values('test 2', '2011-03-02'); insert templates(title, created) values('test 3', '2011-03-03'); insert templates(title, created) values('test 4', '2011-03-04'); insert templates(title, created) values('test 5', '2011-03-05'); insert templates(title, created) values('test 6', '2011-04-01'); insert templates(title, created) values('test 7', '2011-04-02'); insert templates(title, created) values('test 8', '2011-04-03'); insert templates(title, created) values('test 9', '2011-04-04'); insert templates(title, created) values('test 10', '2011-04-05'); … // 300 more 2011-05
the example output of query (based on records "created"-column) is:
m-11-03: 5 5 m-11-04: 5 10 (5 + 5) m-11-05: 300 310 (5 + 5 + 300)
(this spin off of table statistics (aka row count) on time)
this works:
select month, hm, sum(hm) over(order month) from( select to_char(created, 'm-yyyy-mm') month, count(*) hm templates group 1 ) x order month
Comments
Post a Comment