Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 7CA866352E1 for ; Thu, 17 Sep 2009 13:44:50 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 24050-02-2 for ; Thu, 17 Sep 2009 16:44:42 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from gcd.ie (mail1.gcd.ie [83.141.104.148]) by mail.postgresql.org (Postfix) with ESMTP id F2A5F63363A for ; Thu, 17 Sep 2009 13:44:41 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by gcd.ie (Postfix) with ESMTP id 5B92C26A0019 for ; Thu, 17 Sep 2009 17:44:40 +0100 (BST) Received: from gcd.ie ([127.0.0.1]) by localhost (dubmail.gcd.ie [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 16374-09; Thu, 17 Sep 2009 17:44:40 +0100 (BST) Received: from ceartgoleor (unknown [172.16.1.3]) by gcd.ie (Postfix) with ESMTP id 0DF8F26A0011 for ; Thu, 17 Sep 2009 17:44:40 +0100 (BST) Received: by ceartgoleor (Postfix, from userid 1000) id EF3045100AB; Thu, 17 Sep 2009 17:44:39 +0100 (IST) Date: Thu, 17 Sep 2009 17:44:39 +0100 From: Gavin McCullagh To: pgsql-sql@postgresql.org Subject: extracting from epoch values in pgsql Message-ID: <20090917164439.GU16815@gcd.ie> Reply-To: Gavin McCullagh MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.18 (2008-05-17) X-Virus-Scanned: by amavisd-new at gcd.ie X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200909/82 X-Sequence-Number: 33147 Hi folks, I have a db that I need to draw some stats from. The db itself is from the web application moodle which, perhaps to be cross-platform, uses unix epoch times stored as integers throughout (see table description at end of mail). I'd like to query some stats based on the appearance of objects over time, ideally per month. If the "time" were a pgsql timestamp, I'd probably do: SELECT count(id), EXTRACT('month' FROM TIMESTAMP time) AS logmonth, EXTRACT('year' FROM TIMESTAMP time) AS logyear FROM mdl_log WHERE action='login' GROUP BY logmonth,logyear; but it's an epoch time, so I need to convert to a datestamp and then run EXTRACT on that (as far as I can see. I can do the conversion easily enough but I can't then pass that to extract(). I've tried: SELECT EXTRACT('month',(TIMESTAMP WITH TIME ZONE 'epoch' + mdl_log.time * INTERVAL '1 second')) FROM mdl_log; SELECT time, to_timestamp(time) AS ts, EXTRACT('months',to_timestamp(time)) FROM mdl_log; ERROR: syntax error at or near "," LINE 1: ...t time, to_timestamp(time) AS ts, extract('months',to_times... I also tried doing the extract on the alias "ts". Am I doing something wrong here? Is this possible or do I need to approach it in a different way? I've looked through the manual but I can't see a way to convert epoch->timestamp and then use it further. Thanks in advance, Gavin moodle-01-01-2009=# \d mdl_log Table "public.mdl_log" Column | Type | Modifiers --------+------------------------+------------------------------------------------------ id | integer | not null default nextval('mdl_log_id_seq'::regclass) time | integer | not null default 0 userid | integer | not null default 0 ip | character varying(15) | not null default ''::character varying course | integer | not null default 0 module | character varying(20) | not null default ''::character varying cmid | integer | not null default 0 url | character varying(100) | not null default ''::character varying info | character varying(255) | not null default ''::character varying action | character varying(40) | not null default ''::character varying Indexes: "mdl_log_pkey" PRIMARY KEY, btree (id) "mdl_log_act_ix" btree (action) "mdl_log_cmi_ix" btree (cmid) "mdl_log_coursemoduleaction_idx" btree (course, module, action) "mdl_log_tim_ix" btree ("time") "mdl_log_usecou_ix" btree (userid, course)