Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 81B459F9570 for ; Mon, 3 Sep 2007 17:35:16 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 01564-02 for ; Mon, 3 Sep 2007 17:35:06 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from web31801.mail.mud.yahoo.com (web31801.mail.mud.yahoo.com [68.142.207.64]) by postgresql.org (Postfix) with SMTP id E3ECD9F9571 for ; Mon, 3 Sep 2007 17:35:09 -0300 (ADT) Received: (qmail 53656 invoked by uid 60001); 3 Sep 2007 20:35:07 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:Date:From:Subject:To:Cc:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-ID; b=Exd/OYsYvffFjP9Yu4Kx+8+MqDRatgVEanLxHDPtnNfAg5fYh9jtqcEQoqtL5Ke0GXMcb5q365nQgMkA1qR0YLCEncIrJHGer5FIlzs4dwP7m6eZ/9pLt5f+1DtOvfI9N8PqO1qX1HwhDl7hVq1eZI+vP5WzwYM+/ow5tOeL4cg=; X-YMail-OSG: u1q8rnsVM1kjU5Twlg341He19VdBZAbgZsoVO1XJGxvsAVv92_Csd8xJw.QZyyoIc5JqEl224Q-- Received: from [65.211.203.27] by web31801.mail.mud.yahoo.com via HTTP; Mon, 03 Sep 2007 13:35:07 PDT Date: Mon, 3 Sep 2007 13:35:07 -0700 (PDT) From: Richard Broersma Jr Subject: Re: Constraints for grouping To: PostgreSQL Admin Cc: pgsql-sql@postgresql.org In-Reply-To: <46DC5309.8090604@productivitymedia.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Message-ID: <602367.49974.qm@web31801.mail.mud.yahoo.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200709/11 X-Sequence-Number: 29224 --- PostgreSQL Admin wrote: > I have a question that I've thought in my head about using triggers, but > I figure to ask people that do SQL more than I. So, I have a table that > I want two columns. > > (1) A featured column which is for only 1 row, once it switched to > another row than all other rows must be false > > title | author_id | categories | featured > --------------------------------+-----------+------+----- > Thierry Beta Release | 3 | 11 | True > Different Approach | 3 | 11 | > Ottawa Does Not Heart Hip-Hop | 3 | 11 | > > (2) A featured column by category and only allow category_feature equal > to the number of categories. > > Is SQL the way to go (and how - ideas), or should I use python for the > heavy lifting? Are you saying that you have two featured columns? One column for category features and the other for article feature? If I understand you correctly, Item 1 can be achieved nicely with partial indexes. for example, if you only want to allow one row in the entire table to have "feature"= true: CREATE INDEX Only_one_row_true ON Your_table ( featured ) WHERE featured = true; Or if you want to only allow 1 featured article per catagory then: CREATE INDEX Only_one_row_true_per_catagory ON Your_table ( catigories, featured ) WHERE featured = true; This will ensure that the sum( featured = true ) <= sum( unique( catagories )). Regards, Richard Broersma Jr.