X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 9C1639FA5FB for ; Fri, 16 Jun 2006 12:13:28 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 46699-07 for ; Fri, 16 Jun 2006 12:13:22 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from vms048pub.verizon.net (vms048pub.verizon.net [206.46.252.48]) by postgresql.org (Postfix) with ESMTP id A65DD9FA5C3 for ; Fri, 16 Jun 2006 12:13:22 -0300 (ADT) Received: from [192.169.1.2] ([71.101.6.8]) by vms048.mailsrvcs.net (Sun Java System Messaging Server 6.2-4.02 (built Sep 9 2005)) with ESMTPA id <0J0Y00ECRKV2UE00@vms048.mailsrvcs.net> for pgsql-sql@postgresql.org; Fri, 16 Jun 2006 10:11:26 -0500 (CDT) Date: Fri, 16 Jun 2006 11:12:22 -0400 From: Joe Subject: Re: Repetitive code In-reply-to: To: Aaron Bono Cc: pgsql-sql@postgresql.org Message-id: <4492CA56.5020505@freedomcircle.net> Organization: Freedom Circle, LLC MIME-version: 1.0 Content-type: text/plain; charset=ISO-8859-1; format=flowed Content-transfer-encoding: 7bit References: <44920AA7.7070709@freedomcircle.net> <4492B248.7040406@freedomcircle.net> User-Agent: Thunderbird 1.5.0.4 (Windows/20060516) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200606/214 X-Sequence-Number: 25450 Aaron Bono wrote: > I haven't stared at your query as long as you have so I may have missed > something but it looks like in all the selects you are combining the > first column in the select is the column you filter on. So the the > outer query doesn't have to know wiether it is a new or changed row: > > SELECT * FROM ( > SELECT created as my_date, topic_id, 0, 0, 0, 0 FROM topic > WHERE page_type IN (1, 2) > UNION > [snip] > SELECT e.updated as my_date, e.topic_id, 1, 1, entry_id, rel_type > FROM topic_entry e, topic t > WHERE e.topic_id = t.topic_id > AND date_trunc('day', e.updated) != e.created > AND page_type IN (1, 2) > ) my_union > where my_union.my_date >= $dt Thanks Aaron. That does look like a great solution, overlooked since I'm not that familiar with SELECTs in the FROM clause. It may even make it possible to discard the interim table and do the web page/RSS feed directly from the view. > I would almost be tempted to create a view for each small query and name > them something meaningful and then another view that does the union. It > would make the queries easier to understand at least (self documented). That sounds like a good idea too because schema changes would be somewhat insulated by the layered views. Best regards, Joe