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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:09:09+00:00 2026-05-28T19:09:09+00:00

I am facing a problem trying to perform a pivot on the table. A

  • 0

I am facing a problem trying to perform a pivot on the table. A sample of what I want is as shown below.

ProductBarcode    ProductID
--------------    ---------
1000              P1
1001              P1
1002              P2
1003              P3
1004              P4
1005              P4

Now I want to transform the above table into something as below.

ProductID    Barcode1    Barcode2
---------    --------    --------
P1           1000        1001
P2           1002        
P3           1003        
P4           1004        1005

I was trying to work it out with the following query but it wasn’t giving the required results:

SELECT 
  [r1].[productID],
  [r1].[Productbarcode] as Barcode1,
  [r2].[ProductBarcode] as Barcode2
FROM products as r1 right JOIN products as r2 on r1.[productID] = r2.[productID]

Now this is just an example and in actual case, there are hundreds of products which have multiple barcodes.

I have even tried using the following query but all I got was a null in both the barcode columns.

SELECT productID,[barcode1],[barcode2]
FROM
(SELECT barcode, productID
FROM products) as TableToBePivoted
PIVOT
(MAX(barcode)
FOR barcode IN ([barcode1], [barcode2])
) AS PivotedTable;

Any help would be greatly appreciated.

  • 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-05-28T19:09:11+00:00Added an answer on May 28, 2026 at 7:09 pm

    No way to PIVOT without aggregating.

    But here is how to get what you want, enter however many columns (barcodes) you want:

    CREATE TABLE #table1(
        ProductBarcode VARCHAR(10),
        ProductID  VARCHAR(10)
    );
    
    INSERT INTO #table1(ProductBarcode, ProductID)
    VALUES
    ('1000' ,'P1'),
    ('1001' ,'P1'),
    ('1002' ,'P2'),
    ('1003' ,'P3'),
    ('1004' ,'P4'),
    ('1005' ,'P4');
    
    
    WITH T AS(
        SELECT 'Barcode' + RTRIM(LTRIM(STR( ROW_NUMBER() OVER(PARTITION BY ProductID ORDER BY  ProductBarcode)))) AS BarcodeNum,
               ProductBarcode, 
               ProductID    
               FROM #table1
    ) 
    SELECT * FROM T
    PIVOT(MAX(ProductBarcode) FOR BarcodeNum IN([Barcode1], [Barcode2])) P
    

    Results:

    ProductID  Barcode1   Barcode2
    ---------- ---------- ----------
    P1         1000       1001
    P2         1002       NULL
    P3         1003       NULL
    P4         1004       1005
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am facing a problem with .NET generics. The thing I want to do
Im facing problem when trying to apply data annotation. In my case im passing
I am facing a problem with String.CopyTo() method. I am trying to the copy
I am facing a problem in SAX parsing when I am trying to parse
I am trying to parse html page and I am facing a problem which
I am facing a problem with EF 4.1. I am trying to Add a
I am trying to hide an array of elements but facing problem while writing
I am facing this problem while trying to run my java file by writing
Like any other question I am facing problem whenever I am trying to UPDATE
I'm trying to make an arkanoid-like game, and now I'm facing a problem with

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.