Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 4E986632599 for ; Fri, 25 Jun 2010 20:29:08 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 80601-09 for ; Fri, 25 Jun 2010 23:29:00 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-wy0-f174.google.com (mail-wy0-f174.google.com [74.125.82.174]) by mail.postgresql.org (Postfix) with ESMTP id E33CC632445 for ; Fri, 25 Jun 2010 20:29:00 -0300 (ADT) Received: by wyb38 with SMTP id 38so571505wyb.19 for ; Fri, 25 Jun 2010 16:28:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:received:in-reply-to :references:date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=HRsIbmgshF4uJjuI2MhU4+icjrhUxPoPKKdxlNGnn3s=; b=XlmQ15CDCxtj/6KGZwbT/NuSLpNVOfOKmJY60F/f7s4uJXJFy09piGaEDUttE/Ib01 Rur25etIt0MAzuJ5Ccq8OrdrDdz55C/c7nI8KVcwAwJSFE2NWU2ykvCprumfCj+NTH9Y qZV1PTQ6bti6VnKMoL3HHZADzOnIMOzo+9lWI= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=rFCo8lPz24A/LH5gQi91OPvtrxSdNY0FZs9iSte9zDD3152cYnnR/tiKouKAUDQFdn xTmx+CxG/EPTqtU0p+v9X4T33kUjc2jlMJRwwvOG1Bew4V2KCb0gdtunvyUVMwClxHD6 lMvaJqFEL4n7CtafIY5UNPJmKOX/PFxLHyN0Y= MIME-Version: 1.0 Received: by 10.216.170.83 with SMTP id o61mr1104503wel.37.1277508538770; Fri, 25 Jun 2010 16:28:58 -0700 (PDT) Received: by 10.216.2.3 with HTTP; Fri, 25 Jun 2010 16:28:58 -0700 (PDT) In-Reply-To: References: Date: Fri, 25 Jun 2010 20:28:58 -0300 Message-ID: Subject: Re: How do I remove selected words from text field? From: Osvaldo Kussama To: Frank Bax Cc: pgsql-sql@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.8 tagged_above=-5 required=5 tests=BAYES_50=0.8, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201006/100 X-Sequence-Number: 34291 2010/6/25 Frank Bax : > I'm not quite sure how to ask for the query I want, so let's start with > data: > > create table t1 (i int, val varchar); > insert into t1 values(1,'A B C D'); > insert into t1 values(2,'B D E F'); > insert into t1 values(3,'G H I J'); > create table t2 (q varchar, z varchar); > insert into t2 values('A','vowel'); > insert into t2 values('B','consonant'); > insert into t2 values('E','vowel'); > insert into t2 values('K','consonant'); > > t1.val will contain "words" separated by blanks. =A0It might be better if= each > "word" were a separate row in another table; but that's not how the legac= y > database was built. =A0I understand this can be simulated by: > > select i,a[s] from (select i, generate_subscripts(string_to_array(val,' > '),1) as s, string_to_array(val,' ') as a from t1) foo; > > In my "real life" situation, the "words" are not single letters. > > I'd like to write a function that removes selected "words" from t1.val ba= sed > on select on t2. > In the above example; let's exclude all vowels, so I end up with: > 1 'B C D' > 2 'B D F' > 3 'G H I J' > > For some "words" in val; there may not be a row when joining to t2.q; the= se > words must be included in final result. =A0In the above example; there is= no > row in t2 where q=3D"I"; so it is included in result. > > How do I write such a function? =A0Can it be done with SQL only? > Try: SELECT i, array_to_string(array_agg(foo), ' ') "Val" FROM (SELECT * FROM (SELECT i, regexp_split_to_table(val, E'\\s+') AS foo FROM t1) bar LEFT OUTER JOIN t2 ON (bar.foo =3D t2.q) WHERE z IS DISTINCT FROM 'vowel') = foobar GROUP BY i; i | Val ---+--------- 1 | B C D 3 | G H I J 2 | B D F (3 linhas) Osvaldo