What I Have
I have an sql database with several tables interrelated by integer keys. Here are my 3 tables, the column names, and some sample data for each table. Keep in mind that I am just typing this up to give an idea, it is not a direct copy/paste from a database (and thus the formatting is meant to convey the point, not to be readable by an sql database parser.)
Table 1
ItemTable
itemID,itemName, fruitOrVeggie, Color
1, Apple, Fruit, Red
2, Orange, Fruit, Orange
3, Carrot, Vegetable, Orange
Table 2
AttributeTypesTable
attributeID,attributeName
1, Price
2, Weight
3, Diameter
Table 3
ItemAttributesTable
itemID,attributeID,attributeValue
1, 1, .75
1, 2, .5
1, 3, .7
2, 1, .9
2, 3, .7
3, 1, .3
3, 2, .5
Note how I have multiple entries for each itemID in the ItemAttributesTable – this is the part I am trying to consolidate in a new table.
What I Want
From these three tables I want to create a new table like this.
NewTable
itemID,itemName,fruitOrVeggie,Color,Price,Weight,Diameter
1, Apple, Fruit, Red, .75, .5, .7
2, Orange, Fruit, Orange, .9, , .7
3, Carrot, Vegetable, Orange, .3, .5,
In this NewTable, itemID is a unique key so that there is only one entry per itemID – this is the goal. Note how each attributeName is now a column in this new table and how the corresponding data from ItemAttributesTable is now listed here with a single entry for each itemID (leaving a field blank if ItemAttributesTable doesn’t have an entry for that attributeID for that itemID). I do not want to have to hard code in the column names because my actual data has around a dozen columns and I want this query to be versatile enough to be able to keep using it even if an attributeName changes, I add or remove some of them, etc.
How to Get There
I’m mainly looking at the sql involved for this sort of complex query, although a shell of some sort to actually create this new table might be nice. For example, a query and then a Python script that runs that query to create the ItemAttributesTable.
The key parts are how to create a column in a new table based on an entry in another table (in this case, attributeName) and then how to properly pull the data from multiple tables to populate this new table.
In SQLServer2005+ you can use PIVOT operator for rotating a table-valued expression.SELECT…INTO creates a new table and inserts the resulting rows from the query into it
Demo on SQLFiddle
OR
If you have an unknown number of columns(attributeName) to transformation, then you can use a dynamic PIVOT.
Demo on SQLFiddle