Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cekXV-0001OR-GP for pgsql-sql@arkaria.postgresql.org; Fri, 17 Feb 2017 15:33:41 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cekXU-0000hD-TU for pgsql-sql@arkaria.postgresql.org; Fri, 17 Feb 2017 15:33:40 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cekXU-0000h7-8Y for pgsql-sql@postgresql.org; Fri, 17 Feb 2017 15:33:40 +0000 Received: from mwork.nabble.com ([162.253.133.43]) by makus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cekXS-0008Id-0x for pgsql-sql@postgresql.org; Fri, 17 Feb 2017 15:33:39 +0000 Received: from mben.nabble.com (unknown [162.253.133.72]) by mwork.nabble.com (Postfix) with ESMTP id 844ED2D6F5888 for ; Fri, 17 Feb 2017 08:33:37 -0700 (MST) Date: Fri, 17 Feb 2017 08:33:37 -0700 (MST) From: sqlQuestions To: pgsql-sql@postgresql.org Message-ID: <1487345617421-5944908.post@n3.nabble.com> In-Reply-To: References: <1487283597419-5944796.post@n3.nabble.com> <1487286348488-5944801.post@n3.nabble.com> Subject: Re: Conditional Lookup Table with Like MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -0.3 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Hi David, You got me really close, but I'm still not getting the correct results due to what I've learned is called cartesian product. The result set is being multiplied because of extra rows. Have you seen this before? Here is my query with an example schema. It should only return the first 6 rows in table1 with whichever category is correct. SELECT table1.product_code, table1.date_signed, table1.description, CASE WHEN lower(table1.description) LIKE ('%' || lower(table3.lookup_value) || '%') THEN table3.category ELSE table2.category END FROM table1 LEFT JOIN table2 ON table2.psc_code = table1.product_code LEFT JOIN table3 ON table3.psc_code = table1.product_code WHERE date_signed = '2017-02-01'; create table table1 ( product_code int, date_signed timestamp, description varchar(20) ); insert into table1 (product_code, date_signed, description) values (1, '2017-02-01', 'i have a RED car'), (2, '2017-02-01', 'i have a blue boat'), (3, '2017-02-01', 'i have a dark cat'), (1, '2017-02-01', 'i have a green truck'), (2, '2017-02-01', 'i have a blue rug'), (3, '2017-02-01', 'i have a dark dog'), (1, '2017-02-02', 'i REd NO SHOW'), (2, '2017-02-02', 'i blue NO SHOW'), (3, '2017-02-02', 'i dark NO SHOW'); create table table2 ( psc_code int, category varchar(20) ); insert into table2 (psc_code, category) values (1, 'vehicle'), (2, 'vehicle'); create table table3 ( psc_code int, lookup_value varchar(20), category varchar(20) ); insert into table3 (psc_code, lookup_value, category) values (1, 'fox', 'animal'), (1, 'red', 'color'), (1, 'box', 'shipping'), (2, 'cat', 'animal'); -- View this message in context: http://postgresql.nabble.com/Conditional-Lookup-Table-with-Like-tp5944796p5944908.html Sent from the PostgreSQL - sql mailing list archive at Nabble.com. -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql