2

How can I create columns in a SELECT query based on unique values from another column?

For instance I have two tables item and characteristic, an item can have multiple characteristics.

item:

item_id | price
i1        0.13
i2        0.40
i3        0.24
...

characteristic:

char_id | item_id | char_value
c001      i1        2
c498      i1        6
c001      i2        4
c265      i2        1
c378      i3        9
...       ...       ...

What I want the output for the SQL query to be is

item_id | price | c001 | c265 | c378 | c498
i1        0.13    2      null   null   6
i2        0.40    4      1      null   null
i3        0.24    null   null   9      null
...

Keep in mind there can be a lot of different char_ids so manually defining the columns to be c001, c265, c378 and c498 is not an option. Manually defining each column would require me to write hundreds of lines of SQL query.

Is there a way to create an SQL query that can do this?

Ghorich
  • 428
  • 1
  • 4
  • 13

0 Answers0