Re: Results per letter query

From: Dani Castaños <dcastanos(at)androme(dot)es>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Results per letter query
Date: 2007-06-21 10:42:52
Message-ID: 467A562C.2020504@androme.es
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


>> 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)

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message A. Kretschmer 2007-06-21 10:50:45 Re: Results per letter query
Previous Message A. Kretschmer 2007-06-21 10:04:25 Re: Results per letter query