Question

Evening all,

I am attempting to create a table that stores a series of stats on web usage for my application on a daily basis (trivial things like no. new users, total visits etc.), I am currently querying these on the fly, however I would now like to start storing them partly for performance (reducing a load of aggregate querys to single lookup) and partly to allow for historic analysis.

I have come up with the follow basic schema for the table (there will be more columns than this, just to give an idea)

create table web_stats(
    web_stat_id bigserial primary key,
    date_created timestamp not null default now(),
    user_count integer not null,
    new_user_count integer not null
);
comment on table web_stats is 'Table stores statistics on web usage';

Now, I am happy to create the queries to populate the table going forward (I am using Quartz scheduler to run the queries daily)

However I am not so sure the best way to populate the table retrospectively for past dates, should I use an INSERT statement to create a blank row for every day since the application went live (about 2 years ago), then use an UPDATE to populate the blank rows? Or can this be done in one fell swoop? Can someone provide some SQL for creating the rows?

If there is anything wrong with my design assumptions please let me know!

Was it helpful?

Solution

This is how I ended up doing it

INSERT INTO web_stats (date_created)
    SELECT DATE('2011-08-20')+x.id
    FROM generate_series(0,521) AS x(id);

Where 2011-08-20 is the date the application went live, and 521 is the number of days from now until then

This creates the empty table so that I can use the date_created field to populate the other fields using UPDATE statements

Maybe not the most efficient method but it works

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top