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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:13:22+00:00 2026-06-14T01:13:22+00:00

I am using SQL Server 2012, i need help in getting the following output.

  • 0

I am using SQL Server 2012, i need help in getting the following output. My table structure is as follows:

declare @TestTable table (MaterialCode char(5), ItemCode char(5), ItemName varchar(50))

insert into @TestTable values ('AA-01', 'A0001', 'iPhone')
insert into @TestTable values ('AA-02', 'A0001', 'iPad')
insert into @TestTable values ('AA-03', 'A0001', 'iPod')

insert into @TestTable values ('AA-01', 'B0001', 'Galaxy Tab')
insert into @TestTable values ('AA-02', 'B0001', 'Galaxy Note')

insert into @TestTable values ('AA-01', 'C0001', 'Nokia Lumnia')
insert into @TestTable values ('AA-02', 'C0001', 'Motorola')
insert into @TestTable values ('AA-03', 'C0001', 'Samsung S3')
insert into @TestTable values ('AA-04', 'C0001', 'Sony')

--select * from @TestTable

select MaterialCode, ItemCode as [A_ItemCode], ItemName as [A_ItemName] 
from @TestTable where ItemCode='A0001'

select MaterialCode, ItemCode as [B_ItemCode], ItemName as [B_ItemName] 
from @TestTable where ItemCode='B0001'

select MaterialCode, ItemCode as [C_ItemCode], ItemName as [C_ItemName] 
from @TestTable where ItemCode='C0001'

And the output i need should produced from the above three select statements, which should be as follows:

enter image description here

As you can see, when there is no record, NULLs are displayed there. Can anyone help me getting this output. TIA.

EDIT

@JohnLBevan, I tried your pivot approach and when I have another record which is has ItemCode = D0001

insert into @TestTable values ('AA-05', 'D0001', 'Test1')

now even that record is being displayed as

AA-05   NULL    NULL    NULL    NULL    NULL    NULL

How to avoid these type of records.

  • 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-14T01:13:23+00:00Added an answer on June 14, 2026 at 1:13 am

    Not using a pivot, but this gives you the desired results:

    SELECT coalesce(a.MaterialCode, b.MaterialCode, c.MaterialCode) MaterialCode 
    , a.ItemCode A_ItemCode
    , a.ItemName A_ItemName
    , b.ItemCode B_ItemCode
    , b.ItemName B_ItemName
    , c.ItemCode C_ItemCode
    , c.ItemName C_ItemName
    FROM (select * from @TestTable where ItemCode='A0001') a
    full outer join (select * from @TestTable where ItemCode='B0001') b
        on a.MaterialCode = b.MaterialCode
    full outer join (select * from @TestTable where ItemCode='C0001') c
        on a.MaterialCode = c.MaterialCode
        or b.MaterialCode = c.MaterialCode
    

    Here’s an alternate which uses a pivot, though this feels messier:

    SELECT MaterialCode
    , case when [A0001] is null then null else 'A0001' end A_ItemCode
    , [A0001] A_ItemName
    , case when [A0001] is null then null else 'B0001' end B_ItemCode
    , [B0001] B_ItemName
    , case when [A0001] is null then null else 'C0001' end C_ItemCode
    , [C0001] C_ItemName
    FROM @TestTable a
    pivot (
        max(ItemName) 
        for ItemCode in ([A0001],[B0001],[C0001])
    ) pvt
    

    EDIT

    If you want something a little more flexible, here’s a dynamic version:

    declare @dynamicSql nvarchar(max)
    , @dynamicSqlPart nvarchar(max)
    
    select * into #TestTable from @TestTable
    
    set @dynamicSql = 'SELECT MaterialCode ' + CHAR(10)
    select @dynamicSql = @dynamicSql + ', case when [' + ItemCode + '] is null then null else ''' + ItemCode + ''' end ' + ItemCode + '_ItemCode ' + CHAR(10)
        + ', ' + QUOTENAME(ItemCode) + ' ' + ItemCode + '_ItemName ' + CHAR(10)
    , @dynamicSqlPart = ISNULL(@dynamicSqlPart + ',','') +  QUOTENAME(ItemCode)
    from (select distinct ItemCode from @TestTable) x
    
    set @dynamicSql = @dynamicSql + 'from #TestTable a' + CHAR(10)
        + 'pivot (' + CHAR(10)
        + ' max(ItemName) ' + CHAR(10)
        + ' for ItemCode in (' + @dynamicSqlPart + ')' + CHAR(10)
        + ') pvt' + CHAR(10)
    
    print isnull(@dynamicSql,'?')
    exec (@dynamicSql)
    
    drop table #TestTable
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have an issue upgrading to SQL Server 2012. I am using the following
I'm using SQL server 2008 I have the following SQL getting all dates between
I am using Microsoft SQL Server 2012 Express. The following code from MySQL gives
I am using SQL Server 2012 I have a table called AMOUNTS and a
Im using sql server 2012 and its replication feature. Here im trying to manually
I'm using SQL Server 2012 and trying to implement transactional replication. Im using the
I am using a JDBC driver for Microsoft SQL Server 2012 (sqljdbc_4.0) and although
[using SQL Server 2005] I have a table full of users, I want to
Using SQL Server 2000 Table PersonID Date 001 11-02-2009 002 11-02-2009 003 11-02-2009 001
I'm using SQL Server I need to know the number of days that a

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.