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 39CE09FA5BE for ; Thu, 15 Jun 2006 18:41:26 -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 54110-09 for ; Thu, 15 Jun 2006 18:41:24 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from grzm.com (grzm.com [200.46.204.98]) by postgresql.org (Postfix) with ESMTP id 7278F9FA13E for ; Thu, 15 Jun 2006 18:41:24 -0300 (ADT) Received: from localhost (unknown [200.46.208.251]) by grzm.com (Postfix) with ESMTP id 96D26127C4ED for ; Thu, 15 Jun 2006 22:41:20 +0100 (BST) Received: from grzm.com ([200.46.204.98]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 52799-10; Thu, 15 Jun 2006 18:41:13 -0300 (ADT) Received: from [172.16.1.204] (unknown [61.197.227.146]) by grzm.com (Postfix) with ESMTP id 68795127C4EC; Thu, 15 Jun 2006 22:41:09 +0100 (BST) In-Reply-To: <20060615205922.4834.qmail@web33301.mail.mud.yahoo.com> References: <20060615205922.4834.qmail@web33301.mail.mud.yahoo.com> Mime-Version: 1.0 (Apple Message framework v750) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: Cc: "pgsql-sql@postgresql.org" Content-Transfer-Encoding: 7bit From: Michael Glaesemann Subject: Re: SQL Technique Question Date: Fri, 16 Jun 2006 06:41:08 +0900 To: X-Mailer: Apple Mail (2.750) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200606/187 X-Sequence-Number: 25423 On Jun 16, 2006, at 5:59 , wrote: > i frequently join certain tables together in various > tables. > > view the following link for an example: > > http://www.rafb.net/paste/results/mBvzn950.html > > is it a good practice to leave this included in the > queries, as is, or should i factor it out somehow? if > i should factor it, how do i do so? I'm not quite sure what you're getting at in your message (in particular, what is the "this" in "leave this included in the queries"?), and it looks like I have a completely different interpretation of what you're asking, looking at the responses you've already received from Andrew and Rod. I think you're asking about encapsulation and how to efficiently use code, rather than copying and pasting the basic query and then modifying a small portion of it. Based on that interpretation, here's what I'd do. (If I'm wrong, well, then, oh well.) I'd create a view that contains the common code. CREATE VIEW t_inspect_join_view AS SELECT t_inspect_result.inspect_result_pass , t_inspect_result.inspect_result_timestamp , t_product.product_number , t_inspect_result.inspect_result_id FROM t_inspect_result, t_inspect, t_inspect_area, t_serial_number, t_link_contract_number_job_number, t_job_number, t_product WHERE t_inspect_result.inspect_id = t_inspect.inspect_id AND t_inspect.serial_number_id = t_serial_number.serial_number_id AND t_serial_number.link_contract_number_job_number_id = t_link_contract_number_job_number.link_contract_number_job_number_id AND t_link_contract_number_job_number.job_number_id = t_job_number.job_number_id AND t_inspect.inspect_area_id = t_inspect_area.inspect_area_id AND t_product.product_id = t_job_number.product_id; or in a style I find a bit clearer: CREATE VIEW t_inspect_join_view AS SELECT t_inspect_result.inspect_result_pass , t_inspect_result.inspect_result_timestamp , t_product.product_number , t_inspect_result.inspect_result_id FROM t_inspect_result JOIN t_inspect USING (inspect_id) JOIN t_serial_number USING (serial_number_id) JOIN t_link_contract_number_job_number USING (link_contract_number_job_number_id) JOIN t_inspect_area USING (inspect_area_id) JOIN t_job_number USING (job_number_id) JOIN t_product USING (product_id); One of the reasons I like this style is that it makes it easy to see that all of the tables in the FROM clause have join conditions (which is usually what you want). For example, it looks like you probably want the AND t_inspect.inspect_area_id = t_inspect_area.inspect_area_id part of the WHERE clause in your "repeating code" section, so I've added it to the view. With the JOIN conditions (how tables are joined together) now part of the FROM clause, the WHERE clause can be used to list just restrictions restrictions (limiting what rows are returned). I find this much clearer to write and read, as I've got clear separation between these two things. While underneath it all the server might consider everything part of the WHERE clause, sytactically I find this style helpful. I've also added some more columns to the SELECT target list, as you'll want to have them exposed for the extra WHERE clause restrictions. Once part of the view, only columns listed in the SELECT target list will be accessible outside of the view. You may have other restrictions that you want to apply in different cases, so you may want to add more columns to the target list. So your original query, using this view, would look like: SELECT inspect_result_pass FROM t_inspect_join_view WHERE product_number = '7214118000' AND inspect_result_timestamp > '2006-01-01' AND inspect_result_timestamp < '2006-06-13' AND inspect_result_id IN ... Hope this helps. Michael Glaesemann grzm seespotcode net