Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 13A319FB1C6 for ; Tue, 6 Feb 2007 02:58:55 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 55357-10 for ; Tue, 6 Feb 2007 02:58:48 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.171]) by postgresql.org (Postfix) with ESMTP id 138259FA19A for ; Tue, 6 Feb 2007 02:58:50 -0400 (AST) Received: from [194.25.154.138] (helo=webserv.wug.de) by mrelayeu.kundenserver.de (node=mrelayeu5) with ESMTP (Nemesis), id 0ML25U-1HEKHt1mEg-0002XV; Tue, 06 Feb 2007 07:58:49 +0100 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1HEKHZ-00025I-00 for pgsql-sql@postgresql.org; Tue, 06 Feb 2007 07:58:25 +0100 Date: Tue, 6 Feb 2007 07:58:25 +0100 From: "A. Kretschmer" To: pgsql-sql@postgresql.org Subject: Re: Search a range of cases/records Message-ID: <20070206065825.GB6761@a-kretschmer.de> References: <45C253FC.2040002@ucdavis.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <45C253FC.2040002@ucdavis.edu> X-OS: Debian/GNU Linux - weil ich es mir Wert bin! X-Message-Flag: "Windows" is not the answer. "Windows" is the question and the answer is "no"! User-Agent: Mutt/1.5.9i X-Provags-ID: kundenserver.de abuse@kundenserver.de login:51ea9561fc3f6d9539a2acd743e4748c X-Provags-ID2: V01U2FsdGVkX1+uH0xblZHX4fXpweNw4rG9yqcVxw3ztqG4OmWrzS7BMQID7LA/rt0Y+ln0LYlCx8FPbXNW2w0+4K3GiJ/J7i6UyXO95MG144Esu3uqK5gib0CwlEgHVFyU X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200702/46 X-Sequence-Number: 27380 am Thu, dem 01.02.2007, um 12:56:28 -0800 mailte Wei ZOU folgendes: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Greetings: > > I have a dataset of two columns: > price amount > 99.5 10000 > 99.7 8000 > 100 3000 > 100.1 1000 > 100.5 500 > 100.8 1500 > 105 2000 > 200 100 > etc > I have to write a SQL query on how many price tags are within [price+-1] > such as 98.5 to 100.5, 100+-1,etc for each records. > Here I know the price tags counts are 5 for 99.5-100.5, 5 for > 99.7-100.7, 6 for 99-101, etc > How should I do for all of the records? Perhaps something like below: test=*# select s, sum(case when price.price between s-0.5 and s+0.5 then 1 else 0 end) as "+-1", sum(case when price.price between s-1.5 and s+1.5 then 1 else 0 end) as "+-2" from price, generate_series(95, 105) as s group by s order by s; s | +-1 | +-2 -----+-----+----- 95 | 0 | 0 96 | 0 | 0 97 | 0 | 0 98 | 0 | 1 99 | 1 | 5 100 | 5 | 6 101 | 2 | 6 102 | 0 | 2 103 | 0 | 0 104 | 0 | 1 105 | 1 | 1 (11 rows) Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header) GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net