Skip to content
Advertisement

SQL – Database table mapper – Insert into table, based on select and joins

This is a life example of the question so you can understand better what we need.

EXAMPLE

We have 3 tables

For each car item we want to keep these data:

And 3 ‘spec’ values located in ‘specs’ table, based on ‘car_spec’ table:

We want to combine all the required data into one table like this.

Sample data

cars table

car_spec table

spec table

sample result table

What we need

We need to export a new table with the required data.

Can we do this in sql? If not, any suggestions of how can we do it?

Advertisement

Answer

You would typically use conditional aggregation to pivot the specs. The following syntax should work in pretty much all databases:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement