Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id D2E7D9FC005 for ; Thu, 21 Jun 2007 07:43:03 -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 46135-01-4 for ; Thu, 21 Jun 2007 07:42:48 -0300 (ADT) X-Greylist: delayed 01:32:49.579207 by SQLgrey-1.7.4 Received: from fafnir.androme.com (fafnir.androme.com [62.58.96.158]) by postgresql.org (Postfix) with ESMTP id B2C929FBF70 for ; Thu, 21 Jun 2007 07:42:54 -0300 (ADT) Received: by fafnir.androme.com (Postfix, from userid 1003) id C39D5B937C2; Thu, 21 Jun 2007 12:42:52 +0200 (CEST) Message-ID: <467A562C.2020504@androme.es> Date: Thu, 21 Jun 2007 12:42:52 +0200 From: =?ISO-8859-1?Q?Dani_Casta=F1os?= User-Agent: Thunderbird 2.0.0.4 (Windows/20070604) MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: Results per letter query References: <467A406A.3060907@androme.es> <20070621100425.GH1405@a-kretschmer.de> In-Reply-To: <20070621100425.GH1405@a-kretschmer.de> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 200706/158 X-Sequence-Number: 28648 >> 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 > It's exactly what i want. Just one more thing... What if i want also the ones that begin by a non-alphabetical character. In your example: test=*# select * from w; t -------- test foo bar foobar 1foobar /ertw @weras and have: chr | count -----+------- T | 1 B | 1 F | 2 _ | 3 (4 rows)