Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 647B02E2F26 for ; Wed, 5 Mar 2008 13:56:18 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 82688-07-2 for ; Wed, 5 Mar 2008 13:56:10 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from smtpauth.rollernet.us (smtpauth.rollernet.us [208.11.75.5]) by postgresql.org (Postfix) with ESMTP id 28B9F2E2DA4 for ; Wed, 5 Mar 2008 13:56:11 -0400 (AST) Received: from smtpauth.rollernet.us (localhost.localdomain [127.0.0.1]) by smtpauth.rollernet.us (Postfix) with ESMTP id 245A45940A6; Wed, 5 Mar 2008 09:56:04 -0800 (PST) Received: from iron.denterprises.org (ool-4350518d.dyn.optonline.net [67.80.81.141]) by smtpauth.rollernet.us (Postfix) with ESMTP; Wed, 5 Mar 2008 09:56:03 -0800 (PST) Received: from [127.0.0.1] (kell.iron.denterprises.org [192.168.171.80]) by iron.denterprises.org (Postfix) with ESMTP id 92DA1167616; Wed, 5 Mar 2008 12:56:02 -0500 (EST) Message-ID: <47CEDEB1.3050902@denterprises.org> Date: Wed, 05 Mar 2008 12:56:01 -0500 From: Colin Wetherbee User-Agent: Thunderbird 2.0.0.12 (Windows/20080213) MIME-Version: 1.0 To: Jeff Frost CC: pgsql-sql@postgresql.org Subject: Re: finding columns that have three or fewer distinct characters References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Antivirus: avast! (VPS 080305-0, 03/05/2008), Outbound message X-Antivirus-Status: Clean X-Rollernet-Abuse: Processed by Roller Network Mail Services. Contact abuse@rollernet.us to report violations. Abuse policy: http://rollernet.us/abuse.php X-Rollernet-Submit: Submit ID 1245.47cedeb3.cbdfe.0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200803/18 X-Sequence-Number: 30346 Jeff Frost wrote: > I've got an interesting one...I'm trying to find columns that have three > or fewer distinct characters (for example, "aaaaaaaaaa"). Wondering if > I need to write a function or if someone has an idea how to do it with > built in functions and/or pattern matching? > > I think the thing to do would be to lowercase everything, then remove > all duplicate chars and spaces, then use length() on that, but it's not > obvious to me how I might remove the duplicate chars with the pattern > matching support in the docs. It's interesting, indeed. Here's how you might do it with a PL/Perl function. :) CREATE OR REPLACE FUNCTION remove_duplicates(TEXT) RETURNS TEXT AS $$ my ($text) = @_; while ($text =~ s/(.)(.*)\1/$1$2/g != 0) {}; return $text; $$ LANGUAGE plperl; cww=# SELECT remove_duplicates('ffffoooooooobbbbaaaarrrr.!@#$.foobar-baz-qux'); remove_duplicates ------------------- fobar.!@#$-zqux (1 row) Colin