Received: from localhost (unknown [200.46.204.184]) by developer.postgresql.org (Postfix) with ESMTP id 3628D2E01E2 for ; Wed, 30 Apr 2008 18:01:27 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 90464-03-5 for ; Wed, 30 Apr 2008 18:01:19 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from rn-out-0910.google.com (rn-out-0910.google.com [64.233.170.189]) by developer.postgresql.org (Postfix) with ESMTP id 8A8B42E01DE for ; Wed, 30 Apr 2008 18:01:20 -0300 (ADT) Received: by rn-out-0910.google.com with SMTP id i24so482351rng.0 for ; Wed, 30 Apr 2008 14:01:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:from:to:content-type:content-transfer-encoding:mime-version:subject:date:x-mailer; bh=+HhzvDHoNM4IjZdSFEqdwImhkZBVCySRUHSTZi4wx20=; b=H5LhO8G1gMRoVgqZaQvkwADt9wefF7eQvPASDxGWVhtYPGh4n9g55O/L8I3LDZYXqw0kAAwqVRvg4q6ZcjudWFmgyEzW+mJGaY481hBNKDk6vEuB/Vu2W6r4T4szKQi4In/31EgmX6YYaU6ifYq3tp63jssrt6iaGy2I+VQlykg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:from:to:content-type:content-transfer-encoding:mime-version:subject:date:x-mailer; b=nLOE7AoDN32B7CL6/yzxOHKvw72eBeBIh7x+cm4hvhJgMyeMiTv88QQLP5ZNAcajCshOxBz9Oxfu/uirvTDLixcQkWlum3E/XP1XLJOOL7X3/YzvlrH1QNnK0jTG+ar31QjyYYVV6oFwWOLBQPSpJa9uUzNNBtAxAMRupUnH3fg= Received: by 10.142.221.19 with SMTP id t19mr489824wfg.62.1209589276120; Wed, 30 Apr 2008 14:01:16 -0700 (PDT) Received: from ?192.168.1.100? ( [24.19.22.42]) by mx.google.com with ESMTPS id 22sm2860551wfd.19.2008.04.30.14.01.14 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 30 Apr 2008 14:01:15 -0700 (PDT) Message-Id: <824FF9F9-9997-4AD9-9955-3CA658E150D4@gmail.com> From: Alexy Khrabrov To: pgsql-sql@postgresql.org Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit Mime-Version: 1.0 (Apple Message framework v919.2) Subject: columns for count histograms of values Date: Wed, 30 Apr 2008 14:01:13 -0700 X-Mailer: Apple Mail (2.919.2) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200804/226 X-Sequence-Number: 30765 Greetings -- I have a table of the kind Ratings: id integer rating smallint -- where value can take any value in the range 1 to 5. Now I want to have a statistical table Stats of the form id integer min smallint max smallint avg real r1 integer r2 integer r3 integer r4 integer r5 integer -- how can I create it in one pass over Ratings? I can use min(), max(), avg() for insert into stats values (id,select min(rating), max(rating), avg(rating), ...) from ratings -- but what to do for r1,..,r5, short of subselects (select count(rating) from ratings where stats.id=ratings.id) for each, which is an overkill? Also, if a table Stats already exists with some more columns, and we need to do an update, not insert, for the above, how would that work -- update stats set min=min(ratings), ... from ratings where stats.id=ratings.id -- how do we do the histogram in this case, where the id is fixed explicitly? Cheers, Alexy