Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VPZs1-0001JY-Az for pgsql-sql@arkaria.postgresql.org; Fri, 27 Sep 2013 15:22:17 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VPZs0-00038v-Jk for pgsql-sql@arkaria.postgresql.org; Fri, 27 Sep 2013 15:22:16 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VPZrz-00038o-AH for pgsql-sql@postgresql.org; Fri, 27 Sep 2013 15:22:15 +0000 Received: from lrosenman-1-pt.tunnel.tserv8.dal1.ipv6.he.net ([2001:470:1f0e:3ad::2] helo=thebighonker.lerctr.org) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VPZrw-0001LD-Bu for pgsql-sql@postgresql.org; Fri, 27 Sep 2013 15:22:14 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lerctr.org; s=lerami; h=Message-ID:Subject:To:From:Date:Content-Transfer-Encoding:Content-Type:MIME-Version; bh=t7i6iRe4SlHc783S0v5K+Bzr3enFZzOFtu/60kgREUo=; b=YvpF9uLNHKsYk6GWdltH0xS3sWolFC+zt3mcSph5/3NNr97przLr+CLVGe/m5z/uqxl6/d8/VIy0KVASC/XRSFDD0sbsGnQrbA1VI4mHWY3Euf3vep1gFDiYWVQgLTUQUpvWqjqtKhELtj3U9vrdVG4KR0jj0egWzOCK/uSylxs=; Received: from localhost.lerctr.org ([127.0.0.1]:17840 helo=webmail.lerctr.org) by thebighonker.lerctr.org with esmtpa (Exim 4.80.1 (FreeBSD)) (envelope-from ) id 1VPZru-000Mih-4r for pgsql-sql@postgresql.org; Fri, 27 Sep 2013 10:22:11 -0500 Received: from [32.97.110.58] by webmail.lerctr.org with HTTP (HTTP/1.1 POST); Fri, 27 Sep 2013 10:22:09 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Date: Fri, 27 Sep 2013 10:22:09 -0500 From: Larry Rosenman To: pgsql-sql@postgresql.org Subject: Can I simplify this =?UTF-8?Q?somehow=3F?= Message-ID: <4d75971ff9afefca1f715960b59ef986@webmail.lerctr.org> X-Sender: ler@lerctr.org User-Agent: Roundcube Webmail/0.9.4 X-Spam-Score: -5.3 (-----) X-LERCTR-Spam-Score: -5.3 (-----) X-Spam-Report: SpamScore (-5.3/5.0) ALL_TRUSTED=-1, BAYES_00=-1.9, RP_MATCHES_RCVD=-2.426 X-LERCTR-Spam-Report: SpamScore (-5.3/5.0) ALL_TRUSTED=-1, BAYES_00=-1.9, RP_MATCHES_RCVD=-2.426 X-Pg-Spam-Score: 0.7 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org I tried(!) to write this as a with (CTE), but failed. Can one of the CTE experts (or better SQL writer) help me here? -- generate a table of timestamps to match against select generate_series(date_trunc('day',now()-'45 days'::interval),now()+'1 hour'::inte rval,'1 hour') AS thetime into temp table timestamps; -- get a count of logged in users for a particular time SELECT thetime,case extract(dow from thetime) when 0 then 'Sunday' when 1 then 'Monday' when 2 then 'Tuesday' when 3 then 'Wednesday' when 4 then 'Thursday' when 5 then 'Friday' when 6 then 'Saturday' end AS "Day", count(*) AS "#LoggedIn" FROM timestamps,user_session WHERE thetime BETWEEN login_time AND COALESCE(logout_time, now()) GROUP BY thetime ORDER BY thetime; Thanks for any help at all. -- Larry Rosenman http://www.lerctr.org/~ler Phone: +1 214-642-9640 (c) E-Mail: ler@lerctr.org US Mail: 108 Turvey Cove, Hutto, TX 78634-5688 -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql