Conditional Lookup Table with Like

From: sqlQuestions <ryanpgodfrey(at)gmail(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Conditional Lookup Table with Like
Date: 2017-02-16 22:19:57
Message-ID: 1487283597419-5944796.post@n3.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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.

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message David G. Johnston 2017-02-16 22:37:30 Re: Conditional Lookup Table with Like
Previous Message Herwig Goemans 2017-02-09 17:29:00 Re: function source code not returning from inside a pgsql function.