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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:38:39+00:00 2026-05-25T10:38:39+00:00

I am looking for some help on designing a simple pivot so that I

  • 0

I am looking for some help on designing a simple pivot so that I can link it into other parts of my queries.

My data is like this

Items Table

Below is my table if I run Select * from items

ITEM      Weight    
12345         10    
12345         11    
654321        50    
654321        20    
654321       100

There are hundreds of Items in this table but each item code will only ever have
maximum of 3 weight records each.

I want the desired output

ITEM     Weight_1    Weight_2     Weight_3
12345          10          11         null
654321         50          20          100

Would appreciate any suggestions,
I have played around with pivots but each subsequent item puts the weights into weight 4,5,6,7,etc
instead of starting at weight1 for each item.

Thanks

Update

Below is what I have used so far,

SELECT r.*
FROM   (SELECT 'weight' + CAST(Row_number() OVER (ORDER BY regtime ASC)AS
                               VARCHAR(10))
                      line,
               id,
               weight
        FROM   items it) AS o PIVOT(MIN([weight]) FOR line IN (weight1, weight2,
       weight3)) AS r  
  • 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-25T10:38:40+00:00Added an answer on May 25, 2026 at 10:38 am

    You were almost there! You were only missing the PARTITION BY clause in OVER:

    SELECT r.*
    FROM   (SELECT 'weight' + CAST(Row_number() OVER (PARTITION BY id ORDER BY
                                   regtime ASC)
                                          AS
                                                  VARCHAR(10)) line,
                   id,
                   weight
            FROM   items it) AS o PIVOT(MIN([weight]) FOR line IN (weight1, weight2,
           weight3)) AS r  
    

    When you PARTITION BY by ID, the row numbers are reset for each different ID.

    Update

    You do not need dynamic pivot, since you will always have 3 weights. But, if you ever need dynamic number of columns, take a look at some of the examples here:

    • SQL Server PIVOT perhaps?
    • Pivot data in T-SQL
    • How do I build a summary by joining to a single table with SQL Server?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking for some help with a Labview data collection program. If I could collect
I need some help. I am looking for a regex that would match the
I´m looking for some help about my shopping cart. I use a simple script.
I'm looking for some help on a problem that I vaguely inquired about before,
I'm looking for some help in designing a strategy to automate deployment of a
looking for some help:) http://69.65.3.168/~doubleop/pro.sperity/blog is the site i am working on, you can
Im looking for some help regarding to put a save like confirmation if some
Looking for some help in regards to getting my site to understand a link
I am looking for some help on creating a regular expression that would work
I'm looking for some help and direction on how I can pull an image

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.