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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:22:09+00:00 2026-06-04T00:22:09+00:00

I have the below table, Company_ID part_id Serial NO —————— ———– ————– 1 BAU

  • 0

I have the below table,

Company_ID             part_id     Serial NO
------------------    ----------- --------------
1                     BAU          BAU2512  
1                     BAU          BAU3512
1                     BAU          BAU4512
2                     BAU          BAU5512
2                     BAU          BAU6512
2                     BAU          BAU7512

And I want a query to return this

Company_id             Item#1       Item#2         Item#3   
------------------    ---------     -------------  ----------- 
1                     BAU2512       BAU3512        BAU4512
2                     BAU5512       BAU6512        BAU7512

any help with the query appreciated, using SQL Server 2008

To create the sample table.

-- Suppress data loading messages
SET NOCOUNT ON

-- Create Sample Data using a Table Varable
DECLARE @Company TABLE
(Company_ID int,
 part_ID varchar(30),
 SerialNO varchar(30))

-- Load Sample Data
INSERT INTO @Company VALUES (1, 'BAU', 'BAU2512')
INSERT INTO @Company VALUES (1, 'BAU', 'BAU3512')
INSERT INTO @Company VALUES (1, 'BAU', 'BAU4512')
INSERT INTO @Company VALUES (2, 'BAU', 'BAU5512')
INSERT INTO @Company VALUES (2, 'BAU', 'BAU6512')
INSERT INTO @Company VALUES (2, 'BAU', 'BAU7512')
  • 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-04T00:22:10+00:00Added an answer on June 4, 2026 at 12:22 am

    Aha I think there is no way than to use dynamic sql for this. based on the answer here PIVOT in sql 2005

    below is the solution for works for me.

    Create tables(s) etc

    -- Create Sample Data using a Table Varable
    create table dbo.Company
    (Company_ID int,
     part_ID varchar(30),
     SerialNO varchar(30))
    
    -- Load Sample Data
    INSERT INTO Company VALUES (1, 'BAU', 'BAU2512')
    INSERT INTO Company VALUES (1, 'BAU', 'BAU3512')
    INSERT INTO Company VALUES (1, 'BAU', 'BAU4512')
    INSERT INTO Company VALUES (2, 'BAU', 'BAU5512')
    INSERT INTO Company VALUES (2, 'BAU', 'BAU6512')
    INSERT INTO Company VALUES (2, 'BAU', 'BAU7512')
    

    The query

    DECLARE @sql AS varchar(max)
    DECLARE @pivot_list AS varchar(max) -- Leave NULL for COALESCE technique
    DECLARE @select_list AS varchar(max) -- Leave NULL for COALESCE technique
    
    SELECT @pivot_list = COALESCE(@pivot_list + ', ', '') + '[' + CONVERT(varchar, PIVOT_CODE) + ']'
            ,@select_list = COALESCE(@select_list + ', ', '') + '[' + CONVERT(varchar, PIVOT_CODE) + '] AS [col_' + CONVERT(varchar, PIVOT_CODE) + ']'
    FROM (
        SELECT DISTINCT PIVOT_CODE
        FROM (
            SELECT Company_ID, SerialNO, ROW_NUMBER() OVER (PARTITION BY Company_ID ORDER BY SerialNO) AS PIVOT_CODE
            FROM Company
        ) AS rows
    ) AS PIVOT_CODES
    
    SET @sql = '
    ;WITH p AS (
        SELECT Company_ID, SerialNO, ROW_NUMBER() OVER (PARTITION BY Company_ID ORDER BY SerialNO) AS PIVOT_CODE
        FROM Company
    )
    SELECT Company_ID, ' + @select_list + '
    FROM p
    PIVOT (
        MIN(SerialNO)
        FOR PIVOT_CODE IN (
            ' + @pivot_list + '
        )
    ) AS pvt
    '
    
    PRINT @sql
    
    EXEC (@sql)
    

    Answer here:

    http://sqlfiddle.com/#!3/7fd86/1

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

Sidebar

Related Questions

I have table as below ACCOUNT CUSTOMER_ID PAYING_ACCOUNT_ID PARENT_ACCOUNT_ID ACCOUNT_ID COMPANY_ID 24669 24669 24669
Below I have table - Company id name value year 1 IBM 10 2011
I have the below table. I am trying to get tbody column bgcolors matching
I have a table like below: Table A ---------- ID Field1 Field2 Field3 1
i have the table below <tr id=group_1>...</tr> <tr id=el>...</tr> <tr id=el>...<tr> <tr id=group_2></tr> <tr
I have a Generic List object which hold below table as its own value.
I have a mysql database with a table structure like below : Table Name
I have the below column on my table what gets binded on pageload, the
I have an SQL table defined as below: CREATE TABLE [TestComposite] ( ID int,
I have a history table like below CREATE TABLE [dbo].[t_PettyCash_History] ( [TableName] [varchar] (500)

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.