Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ceUQ8-0007k1-CK for pgsql-sql@arkaria.postgresql.org; Thu, 16 Feb 2017 22:21:00 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1ceUQ7-0002L6-SG for pgsql-sql@arkaria.postgresql.org; Thu, 16 Feb 2017 22:20:59 +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 1ceUPA-0000QD-BH for pgsql-sql@postgresql.org; Thu, 16 Feb 2017 22:20:00 +0000 Received: from mwork.nabble.com ([162.253.133.43]) by makus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ceUP7-0004OW-S3 for pgsql-sql@postgresql.org; Thu, 16 Feb 2017 22:19:59 +0000 Received: from mben.nabble.com (unknown [162.253.133.72]) by mwork.nabble.com (Postfix) with ESMTP id 80CF12D48CA1C for ; Thu, 16 Feb 2017 15:19:57 -0700 (MST) Date: Thu, 16 Feb 2017 15:19:57 -0700 (MST) From: sqlQuestions To: pgsql-sql@postgresql.org Message-ID: <1487283597419-5944796.post@n3.nabble.com> Subject: 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 I'm having trouble with a weird query. *table1* code description category *table2* code lookup_value category I want to pull the code, description and category from table1. Some, not all, codes from table1 have entries in table2, and some have multiple. In table2, where codes are equal, and when the lookup_value string is found inside the description, I want to pull the category from table2 instead of the category from table1. I was thinking about a case statement, but can't figure out the syntax. I appreciate any ideas that would help me out. Thanks a lot! SELECT table1.code, table1.description, CASE WHEN EXISTS ( SELECT 1 FROM table1, table2 WHERE table1.code = table2.code AND table1.description LIKE '%table2.lookup_value%' LIMIT 1 ) THEN table2.category ELSE table1.category END AS category FROM table1 -- View this message in context: http://postgresql.nabble.com/Conditional-Lookup-Table-with-Like-tp5944796.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