0

I have a table which looks something like this:

| id | fun_data     | value |
|----+--------------+-------|
|  1 | {A1, B1, B2} | foo   |
|  2 | {A1, B1, C1} | bar   |

I wish to construct a query using another array which will match all rows of the table according to the following rules:

  1. Input array contains at most one code of each category (A/B/C)
  2. Query must return rows where there are matching codes for each category

For example:

  • input (A1, B1) matches only row 1
  • input (A1, B2) matches only row 1
  • input (A1, B1, C1) matches rows 1 and 2
  • input (A1, C1) matches no rows

I was thinking I should first construct an intermediate table like this:

| fun_data     | value |
|--------------+-------|
| {A1, B1}     | foo   |
| {A1, B2}     | foo   |
| {A1, B1, C1} | bar   |

And then query it with:

SELECT value FROM /* intermediate table goes here here */
WHERE fun_data <@ input_data;

How can I construct this intermediate table?

Is there a better approach?

msladecek
  • 21
  • 1
  • 5

0 Answers0