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

  • Home
  • SEARCH
  • 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 9127911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:23:39+00:00 2026-06-17T07:23:39+00:00

Hi I have the following table: CREATE TABLE #Test ( ProductID int, MainMasterFeatureID int,

  • 0

Hi I have the following table:

CREATE TABLE #Test
        (
        ProductID int,
        MainMasterFeatureID int,
        --MasterFeatureValue Varchar(100),
        ChilFeatureName varchar(100),
        ParentFeatureName varchar(100)
    )

    INSERT INTO #Test
    SELECT 40,1,,'Pack','Type' 
    UNION ALL 
    SELECT 40,0,'Laminate','Technology'
    UNION ALL
    SELECT 40,11,'Yes','Coated'
    UNION ALL
    SELECT 52,1,'Roll','Type'
    UNION ALL
    SELECT 52,11,'NO','Coated'




    SELECT * FROM #Test


 CREATE TABLE #tProduct
    (
        tProductID int PRIMARY KEY, 
        tProductCode nvarchar(128), 
        tProductName nvarchar(256)
    )
INSERT INTO #tProduct
SELECT 40,'001','ABC'
UNION ALL
SELECT 52,'002','XYZ'
UNION ALL
SELECT 50,'006','IJK' 

From #test table I want to generate result like this:

 ProductID          Type             Technology      Coated 

   40               Pack              Laminate          YES 

   52               Roll               Null              No

Here type,Technology,Coated are not fixed this can be generated dynamically..

It can be generated like this…

Declare @sql Varchar(MAX) 
Select @sql  = 'SELECT ProductID, ' 
Select @sql = @sql + STUFF((Select DISTINCT ',MAX(Case When ParentFeatureName = ' + CHAR(39) + ParentFeatureName + CHAR(39) + ' Then ChilFeatureName Else ' + CHAR(39) + CHAR(39) + ' End) As ' + ParentFeatureName From #Test For XML PATH('')),1,1,'') 
Select @sql = @sql + ' FROM #Test Group By ProductID' 
Execute(@sql)

Now I want to join this dynamic query to #tProduct to get the desired results:

 tProductID   tProductName        Type             Technology      Coated 

   40            ABC              Pack              Laminate          YES 

   52            XYZ              Roll               Null              No  
  • 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-17T07:23:41+00:00Added an answer on June 17, 2026 at 7:23 am

    You can do this with a dynamic pivot and a join in the dynamic query to #tProduct

    declare @SQL nvarchar(max)
    declare @FieldList nvarchar(max)
    
    set @FieldList = 
      (
      select distinct ','+quotename(ParentFeatureName)
      from #Test
      for xml path(''), type
      ).value('substring(text()[1], 2)', 'nvarchar(max)')
    
    set @SQL = 
    '
    select TP.tProductID,
           TP.tProductName,'+
           @FieldList+'
    from 
      (
      select ProductID, 
             ParentFeatureName,
             ChilFeatureName
      from #Test
      ) as T
    pivot
      (
        max(T.ChilFeatureName) 
        for T.ParentFeatureName in ('+@FieldList+')
      ) as P
    inner join #tProduct as TP
      on P.ProductID = TP.tProductID'
    
    exec(@SQL)
    

    SQL Fiddle

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

Sidebar

Related Questions

I have the following test-code: CREATE TABLE #Foo (Foo int) INSERT INTO #Foo SELECT
I have the following table: CREATE TABLE TEST(ID TINYINT NULL, COL1 CHAR(1)) INSERT INTO
I have the following table/indexes - CREATE TABLE test ( coords geography(Point,4326), user_id varchar(50),
I do have the following table: create table test_seq (id int, obs int); insert
I have the following table and data: CREATE TABLE `test` ( `id` int(11) NOT
I have a table defined by the following SQL: CREATE TABLE test ( id
I have the following table CREATE TABLE `prod_prices` ( `id` varchar(32) COLLATE utf8_unicode_ci NOT
I have the following table: create table ARDebitDetail(ID_ARDebitDetail int identity, ID_Hearing int, ID_AdvancedRatePlan int)
I have the following table: CREATE TABLE IF NOT EXISTS `customer_list` ( `id` INT
Let's say we have the following table: CREATE TABLE T ( ID INT, String1

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.