Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TKYDQ-0001Ww-Hb for pgsql-sql@postgresql.org; Sat, 06 Oct 2012 17:31:04 +0000 Received: from smtp102.prem.mail.ac4.yahoo.com ([76.13.13.41]) by magus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1TKYDM-0007FO-L1 for pgsql-sql@postgresql.org; Sat, 06 Oct 2012 17:31:03 +0000 Received: (qmail 6666 invoked from network); 6 Oct 2012 17:30:58 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=DKIM-Signature:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=6t/f+yW7wo4PtN84hYiAQ5FFMAr/EHOtPzyYSv5xOxToAAUNRXk5ervt3CTxxTFsiltLqQFO2rNOcSMw8GoNNtucXup9kCGFs29xbKNGvLo9yB4Zu0gB+MNpeahmuQarAno/adwocxNeCpYVKPKOJ9+fAcJssJ8feOrZguZCv3k= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1349544658; bh=sqozqbg6Mmee7WovZpYIYtwbWfQ3jorDJQBB65JbZLw=; h=X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=zuLWytdTAq9LZn5Hf82t6jNAUOQplAG0prIAGNTJj5agnzB1r00jhsHteAvpZCfg/cgRO+HCBuEX4GK6A5knl5/MnUgusUzfxJAqq17/SXoH+hDOi+MklB7l8/bC8pfd66Ft1fV1JwhaD+EGHlQ0Jyy5xywnAN9i7OocmY6wfDU= X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: kBmRBuIVM1mhsgcjlb7_5RRL3Rz1b3zS.oVW2j8ol4xPLS8 gNwzM3N9PHaUY.4vun9m.fA06vj1In_FYoHSVOj4E9tsjcJv95RPUznWlvRd PpMrV95QukKKEldoQI72IbTt.PmuOaQnbmJuOgUFDea2RmVqq4.L9Lbz0kqo YfnJquKUaviHmM9TvvHAe4Qx4LIeF3_kkEc3QEYhuMkyqdeNIG81fNmJhRPE KPviFO6fza6e0PNvJh3kmwloUX.iWkVBW7EosWNYIg6yVdKZX26QOmOIRTvb rxfoQHq4koA_0xXsQdjEmCycWTk2mWynjRik3ZMPja_olcSBhN50dUZempY0 23R3DShuaQ68qA6gzVtn0HOIAtzrsdrwO60wT9DzoavEsyExaKVGoMMYuvuu 8Vg0gbnjkEOjIlRfQrRwH5NTHJfHntpSZREyJRtASjLib2UonMBMstzdMiat rPnRU X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from WolfDog (polobo@24.93.23.188 with login) by smtp102.prem.mail.ac4.yahoo.com with SMTP; 06 Oct 2012 17:30:58 +0000 UTC From: "David Johnston" To: "'air'" , References: <1349527666985-5726792.post@n5.nabble.com> In-Reply-To: <1349527666985-5726792.post@n5.nabble.com> Subject: Re: How to make this CTE also print rows with 0 as count? Date: Sat, 6 Oct 2012 13:30:36 -0400 Message-ID: <00fa01cda3e8$4c86df60$e5949e20$@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQHuO4ziftcHZQr3k2KdtnOT6cOOSJdrVVmA Content-Language: en-us X-Pg-Spam-Score: -4.1 (----) X-Archive-Number: 201210/28 X-Sequence-Number: 36899 > -----Original Message----- > From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql- > owner@postgresql.org] On Behalf Of air > Sent: Saturday, October 06, 2012 8:48 AM > To: pgsql-sql@postgresql.org > Subject: [SQL] How to make this CTE also print rows with 0 as count? > > I have a CTE based query, to which I pass about 2600 4-tuple > latitude/longitude values using joins - these latitude longitude 4-tuples have > been ID tagged and held in a second table called coordinates. These top left > and bottom right latitude / longitude values are passed into the CTE in order > to display the amount of requests (hourly) made within those coordinates > for given two timestamps).- I am able to get the total requests per day within > the timestamps given, that is, the total count of user requests on every > specified day. (E.g. user opts to see every Wednesday or Wednesday AND > Thursday etc. - between hours 11:55 and 22:04 between dates January 1 and > 31, 2012 for every latitude/longitude 4-tuples I pass.) But I cannot view the > rows with count 0. My query is as below: > > > > WITH v AS ( > SELECT '2012-01-1 11:55:11'::timestamp AS _from > ,'2012-01-31 22:02:21'::timestamp AS _to > ) > , q AS ( > SELECT c.coordinates_id > , date_trunc('hour', t.calltime) AS stamp > , count(*) AS zcount > FROM v > JOIN mytable t ON t.calltime BETWEEN v._from AND v._to > AND (t.calltime::time >= v._from::time AND > t.calltime::time <= v._to::time) AND (extract(DOW from > t.calltime) = 3) > JOIN coordinates c ON (t.lat, t.lon) > BETWEEN (c.bottomrightlat, c.topleftlon) > AND (c.topleftlat, c.bottomrightlon) > GROUP BY c.coordinates_id, date_trunc('hour', t.calltime) > ) > , cal AS ( > SELECT generate_series('2011-2-2 00:00:00'::timestamp > , '2012-4-1 05:00:00'::timestamp > , '1 hour'::interval) AS stamp > FROM v > ) > SELECT q.coordinates_id, cal.stamp::date, sum(q.zcount) AS zcount > FROM v, cal > LEFT JOIN q USING (stamp) > WHERE extract(hour from cal.stamp) >= extract(hour from v._from) > AND extract(hour from cal.stamp) <= extract(hour from v._to) > AND extract(DOW from cal.stamp) = 3 > AND cal.stamp >= v._from > AND cal.stamp <= v._to > GROUP BY q.coordinates_id, cal.stamp::date ORDER BY q.coordinates_id, > stamp; > > > > > The output I get when I execute this query is basically like this (normally I > have about 10354 rows returned excluding the rows with 0 zcount, just > providing two coordinates for sake of similarity): > > coordinates_id | stamp | zcount > 1 ;"2012-01-04"; 2 > 1 ;"2012-01-11"; 3 > 1 ;"2012-01-18"; 2 > 2 ;"2012-01-04"; 2 > 2 ;"2012-01-11"; 3 > 2 ;"2012-01-18"; 2 > > > > > However, it should be like this where all rows with zcount 0 should also be > printed out along with rows that have nonzero zcounts -E.g. January 25 with > zcount 0 for the two coordinates with ID 1 and 2 should also be printed in this > small portion of example-: > > coordinates_id | stamp | zcount > 1 ;"2012-01-04"; 2 > 1 ;"2012-01-11"; 3 > 1 ;"2012-01-18"; 2 > 1 ;"2012-01-25"; 0 > 2 ;"2012-01-04"; 2 > 2 ;"2012-01-11"; 3 > 2 ;"2012-01-18"; 2 > 2 ;"2012-01-25"; 0 > > > > > How can I achieve this? Thanks in advance. > > Food for thought, generally when you want "everything including the zeros" you want to build the "master" set without any values, build out the "values only" dataset, then LEFT JOIN them and use COALESCE to generate values for the missing data. So: SELECT id, stamp, COALESCE(datavalues.zcount, 0) AS zcount FROM (cal CROSS JOIN id_master) master LEFT JOIN datavalues USING (id, stamp) Also, the mixing of multiple FROM relations and JOINs is confusing. In particular is the fact the JOIN takes precedence over the "," in FROM "A JOIN clause combines two FROM items. Use parentheses if necessary to determine the order of nesting. In the absence of parentheses, JOINs nest left-to-right. In any case JOIN binds more tightly than the commas separating FROM items." http://www.postgresql.org/docs/9.2/interactive/sql-select.html Your query is equivalent to: SELECT ... FROM v CROSS JOIN (cal LEFT JOIN q USING stamp) WHERE ... Anyway, the "create master, left join data, coalesce" methodology is one that I find to be easy to understand and implement. HTH, David J.