Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9257577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:11:05+00:00 2026-06-18T12:11:05+00:00

What I Have I have an sql database with several tables interrelated by integer

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:11:06+00:00Added an answer on June 18, 2026 at 12:11 pm

    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

    IF OBJECT_ID('NewTable') IS NOT NULL DROP TABLE NewTable
    SELECT ItemID, ItemName, FruitOrVeggie, Color, Price, Weight, Diameter
    INTO NewTable
    FROM      
    (      
     SELECT t.ItemID, t.ItemName, t.FruitOrVeggie, Color, attributeName, attributeValue
     FROM ItemTable t JOIN ItemAttributesTable at ON t.ItemID = at.ItemID
                      JOIN AttributeTypesTable tt ON at.attributeID = tt.attributeID
    ) x
    PIVOT
    (
     MAX(attributeValue) FOR attributeName IN ([Price], [Weight], [Diameter])
     ) p
    
    SELECT *
    FROM NewTable
    

    Demo on SQLFiddle

    OR

    If you have an unknown number of columns(attributeName) to transformation, then you can use a dynamic PIVOT.

    DECLARE @cols AS nvarchar(max),
            @query AS nvarchar(max)
    
    SELECT @cols = STUFF((SELECT DISTINCT ',' + QUOTENAME(attributeName)
                          FROM AttributeTypesTable
                          FOR XML PATH(''), TYPE).value('.', 'nvarchar(max)'), 1, 1, '')
    
    IF OBJECT_ID('NewTable') IS NOT NULL DROP TABLE NewTable                      
    SET @query = 'SELECT ItemID, ItemName, FruitOrVeggie, Color, ' + @cols + 
                 'INTO NewTable FROM 
                 (
                  SELECT t.ItemID, t.ItemName, t.FruitOrVeggie, Color, attributeName, attributeValue
                  FROM ItemTable t JOIN ItemAttributesTable at ON t.ItemID = at.ItemID
                                   JOIN AttributeTypesTable tt ON at.attributeID = tt.attributeID
                  ) x
                  PIVOT
                  (
                   MAX(attributeValue) FOR attributeName IN (' + @cols + ')
                   ) p '
    
    EXEC(@query)
    
    SELECT *
    FROM NewTable 
    

    Demo on SQLFiddle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL Server 2005 database with several tables. One of the tables
I have a SQL database that has several hundred tables in it, but I
I have a small SQL CE 4.0 database with several tables, mapped using Entity
In my SQL Server 2008 Enterprise I have a database that contains several tables:
I have several tables in my database and I am using the SQL Server
I have a SQL Server 2005 database from which I'm removing several large tables
I have a SQL Server Database with several tables. One of them has ID
I have a SQL database which consists of several tables like table 1 table
I have the requirement to search several different tables in my SQL Server database.
I have a SQL Server database which is shared between several ASP.NET (VB.NET) websites,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.