Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 649BF9FB66D for ; Thu, 21 Jun 2007 07:04:33 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 03946-05 for ; Thu, 21 Jun 2007 07:04:24 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.187]) by postgresql.org (Postfix) with ESMTP id 5F2649FBFED for ; Thu, 21 Jun 2007 07:04:27 -0300 (ADT) Received: from [194.25.154.138] (helo=webserv.wug.de) by mrelayeu.kundenserver.de (node=mrelayeu3) with ESMTP (Nemesis), id 0MKxQS-1I1JWb2bpD-00087L; Thu, 21 Jun 2007 12:04:26 +0200 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1I1JWb-0002gA-00 for pgsql-sql@postgresql.org; Thu, 21 Jun 2007 12:04:25 +0200 Date: Thu, 21 Jun 2007 12:04:25 +0200 From: "A. Kretschmer" To: pgsql-sql@postgresql.org Subject: Re: Results per letter query Message-ID: <20070621100425.GH1405@a-kretschmer.de> References: <467A406A.3060907@androme.es> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <467A406A.3060907@androme.es> 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: V01U2FsdGVkX191h/U8cU5horX4SFauZR7v9qrUBZic5osEoK1 S5A279yrmU+tzyZoKsVPTMIhnHliYIpAIg/Mv6cRusxBGXu6JG XXC3cgiSGq2il2+C/GFYFgTt1l47pbZ X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200706/157 X-Sequence-Number: 28647 am Thu, dem 21.06.2007, um 11:10:02 +0200 mailte Dani Castaņos folgendes: > Hi! > > I'm trying to build a query to get if there is an occurrence for a field > for each alphabetical letter. > My first thought to know it was to do something like: > > SELECT COUNT(field) FROM table WHERE UPPER( field ) LIKE UPPER( 'A%' ) > LIMIT 1; > SELECT COUNT(field) FROM table WHERE UPPER( field ) LIKE UPPER( 'B%' ) > LIMIT 1; > SELECT COUNT(field) FROM table WHERE UPPER( field ) LIKE UPPER( 'C%' ) > LIMIT 1; > ... > and so on... > > Is there any way to do it in only one query?? I'm not sure if i understand you correctly, sorry, if not. test=*# select * from w; t -------- test foo bar foobar (4 rows) test=*# select chr(x), count(1) from generate_series(65,90) x, w where upper(substring (w.t from 1 for 1)) ~ chr(x) group by 1; chr | count -----+------- T | 1 B | 1 F | 2 (3 rows) Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header) GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net