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 8800213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:31:38+00:00 2026-06-14T00:31:38+00:00

I’ve never done something like this, and I tried finding it on google but

  • 0

I’ve never done something like this, and I tried finding it on google but without result.

I have 3 tables that look like this:

Orders:

OdredID (int) PK,
UserID (int) FK,
OdredDate (datetime)

Components:

ComponentID (int) PK,
Name (nvarchar(50)),
Type (nvarchar(max))

OrderComponent:

OrderComponentID (int) PK,
OrderID (int) FK,
ComponentID (int) FK,
Value (nvarchar(max))

Lets say one order has 3 components with names: [CPU, Motherboard, Memory] and values [1GHz, AsusP5, 2GB Kingston DDR3]

I need a result has columns like this:

OrderID  UserID   Date          CPU    Motherboard   Memory
   1        1     2012-05-21    1GHz   AsusP5        2GB Kingston DDR3

Basically I need every join row to go as new column with name taken from Name column of join table and value from Value column.

  • 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-14T00:31:39+00:00Added an answer on June 14, 2026 at 12:31 am

    Try this:

    SELECT
      o.orderid,
      o.userid,
      MAX(CASE WHEN c.Name = 'CPU' THEN oc.Value END) AS 'CPU',
      MAX(CASE WHEN c.Name = 'Motherboard' THEN oc.Value END) AS 'Motherboard',
      MAX(CASE WHEN c.Name = 'Memory' THEN oc.Value END) AS 'Memory'
    FROM orders o
    INNER JOIN ordercomponents oc ON c.orderid = oc.orderId
    INNER JOIN Components c ON oc.componentid = c.componentid
    GROUP BY o.orderid, o.userid
    

    SQL Fiddle Demo

    Note that: This the standard SQL way to do this. But you can use the SQL Server PIVOT table operator to do the same thing like so:

    SELECT  *
    FROM
    (
       SELECT o.orderid, o.userid, c.Name 'Name', oc.value 'value'
       FROM orders o
       INNER JOIN ordercomponent oc ON o.orderid = oc.orderId
       INNER JOIN Components c ON oc.componentid = c.componentid
     ) t 
    PIVOT
    (
      MAX(value)
      FOR Name IN ([CPU], [Motherboard], [Memory])
    ) p;
    

    But this for a set of pre defined values like [CPU], [Motherboard], [Memory].

    For unknown number of values, you have to do it dynamically like so:

    DECLARE @cols AS NVARCHAR(MAX);
    DECLARE @query AS NVARCHAR(MAX);
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.name) 
                        from Components c
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT orderid, userid  ' + @cols + ' from 
                 (
                     select o.orderid, o.userid, c.Name Name, oc.value value
                     FROM orders o
                     INNER JOIN ordercomponent oc ON o.orderid = oc.orderId
                     INNER JOIN Components c ON oc.componentid = c.componentid
                ) x
                pivot 
                (
                    max(value)
                    for name in (' + @cols + ')
                ) p '
    
    execute(@query);
    

    Updated SQL Fiddle Demo

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.