I am new to joining two tables in PostgreSQL.
Table 1: O_REMEDY_PEOPLEGROUP
manager_name | manager_hierarchy |
---|---|
farhanpat | raju jacob watson |
soumyvant | neha william |
amitk | piyush shawn |
brian | david warner |
Table 2: Org_details
org_name | vp_details |
---|---|
org1 | jacob |
org2 | piyush |
org3 | neha |
We want to search for each VP if it’s present in the manager hierarchy of table 1.
If present, third column should report org_name
, else null.
Output Table:
manager_name | manager_hierarchy | org_name |
---|---|---|
farhanpat | raju jacob watson | org1 |
soumyvant | neha william | org3 |
amitk | piyush shawn | org2 |
brian | david warner | null |
2
Answers
As already pointed data stored as comma separated list will bring more and more troubles.
See Is storing a delimited list in a database column really that bad?
As per the question.
In your particular case you have stored not only a comma separated string but
[
,]
symbols and a space between ‘words’.So FIND_IN_SET cant be used without doing some replace.
The following query replace the above symbols and the space with empty.
Try
Another options is using LIKE
See example
Note. No index will be used in either query, so performance for large data set will be terrible
While stuck on your unfortunate design with multiple values concatenated as single string, this should work as efficiently as it gets (while being accurate):
fiddle
Since it’s undefined in the question I pick the first arbitrary match with
LIMIT 1
. (There might be multiple.)If
org_details
isn’t trivially small, there should be an index on(vp_details)
to get index scans, or even on(vp_details, org_name)
to get index-only scans.