0

I have a table that can hold the id for entries of another table and I need to convert the rows from the second table into columns for a new view based on the first table. It is similar to what is done in this question, but I need it to be dynamic, i.e. instead of manually choosing email1, email2 and email3, it needs to get the rows from the second table and create columns based on that, which will hold boolean values if the row holds the id for the column.

For example, consider table A as

entry_id tags
1uxs2NM7K6nx58z13WFiNq descriptionReview
1v5AKF9Z5VmBdAzObr0Xxs titleReview
1ZLV74fhn2kDqYi7R3XxEW descriptionReview;titleReview

and table B as

id
descriptionReview
titleReview
subtitleReview

The resulting view would look like this

entry_id descriptionReview titleReview subtitleReview
1uxs2NM7K6nx58z13WFiNq TRUE FALSE FALSE
1v5AKF9Z5VmBdAzObr0Xxs FALSE TRUE FALSE
1ZLV74fhn2kDqYi7R3XxEW TRUE TRUE FALSE
Minoru
  • 1,658
  • 3
  • 19
  • 41
  • 1
    Pivoting rows to columns turns data (in your case `descriptionReview`, `titleReview`, and `subtitleReview`) into schema (the columns of your output). Doing this dynamically is not possible with SQL because the query defines the columns to be output. You can write some code to generate your query or use a host language to accomplish this pivot. – Mike Organek Aug 23 '21 at 16:12

0 Answers0