X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id C44CC54431 for ; Sat, 12 Mar 2005 16:19:02 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 65006-08 for ; Sat, 12 Mar 2005 16:18:58 +0000 (GMT) Received: from wolff.to (wolff.to [66.93.249.74]) by svr1.postgresql.org (Postfix) with SMTP id 4C2425440A for ; Sat, 12 Mar 2005 16:18:57 +0000 (GMT) Received: (qmail 22484 invoked by uid 500); 12 Mar 2005 16:34:57 -0000 Date: Sat, 12 Mar 2005 10:34:57 -0600 From: Bruno Wolff III To: Tambet Matiisen Cc: pgsql-sql@postgresql.org Subject: Re: Parameterized views proposition Message-ID: <20050312163457.GC22317@wolff.to> Mail-Followup-To: Bruno Wolff III , Tambet Matiisen , pgsql-sql@postgresql.org References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.011 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200503/169 X-Sequence-Number: 20648 On Sat, Mar 12, 2005 at 13:40:30 +0200, Tambet Matiisen wrote: > Hi there! > > We use views in our applications a lot, in fact we prefer to have least > sql at client side. All queries are written as select * from view, > whenever possible. > > But there are queries, which are impossible to express as views. I don't think this is literally what you mean, since any select query can be made into a view. What may be hard is creating a simple view where you can supply parameters to the view. This is especially going to be true if you want to use * to select the columns and don't want extra columns that you might need to paramterize the view. > Especially if you would like to put a filter on right side of left join. > Consider this query: > > select p.product_id, coalesce(sum(s.amount), 0) > from product p > left join sales s on p.product_id = s.product_id and s.date between > '2005-01-01' and '2005-01-31' > group by p.product_id > > We would like to have all products listed with sum of their sales or 0 > if there wasn't any. I haven't figured out so far, how to write this > query as view, so that I can set different filters at client side. You need to expose the columns you want to filter on so that they can be used in WHERE clauses.