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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:29:11+00:00 2026-05-23T05:29:11+00:00

I have the following data: ID Data 1 tera 1 add 1 alkd 2

  • 0

I have the following data:

ID       Data
1         tera
1         add
1         alkd
2         adf
2         add
3         wer
4         minus
4         add
4         ten

I am trying to use a pivot table to push the rows into 1 row with multiple columns per ID.
So as follows:

ID   Custom1     Custom2    Custom3   Custom4..........
1      tera       add         alkd      
2      adf        add
3      wer
4      minus      add          ten

I have the following query so far:

  INSERT INTO @SpeciInfo
(ID, [Custom1], [Custom2], [Custom3], [Custom4], [Custom5],[Custom6],[Custom7],[Custom8],[Custom9],[Custom10],[Custom11],[Custom12],[Custom13],[Custom14],[Custom15],[Custom16])
    SELECT 
        ID,
        [Custom1],
        [Custom2],
        [Custom3],
        [Custom4],
        [Custom5],
        [Custom6],
        [Custom7],
        [Custom8],
        [Custom9],
        [Custom10],
        [Custom11],
        [Custom12],
        [Custom13],
        [Custom14],
        [Custom15],
        [Custom16]
    FROM SpeciInfo) p
    PIVOT
    (
        (
            [Custom1],
            [Custom2], 
            [Custom3], 
            [Custom4],
            [Custom5],
            [Custom6],
            [Custom7],
            [Custom8],
            [Custom9],
            [Custom10],
            [Custom11],
            [Custom12],
            [Custom13],
            [Custom14],
            [Custom15],
            [Custom16]
        )
    ) AS pvt
    ORDER BY ID;

I need the 16 fields, but I am not exactly sure what I do in the From clause or if I’m even doing that correctly?

Thanks

  • 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-23T05:29:12+00:00Added an answer on May 23, 2026 at 5:29 am

    If what you seek is to dynamically build the columns, that is often called a dynamic crosstab and cannot be done in T-SQL without resorting to dynamic SQL (building the string of the query) which is not recommended. Instead, you should build that query in your middle tier or reporting application.

    If you simply want a static solution, an alternative to using PIVOT of what you seek might look something like so in SQL Server 2005 or later:

    With NumberedItems As
        (
        Select Id, Data
            , Row_Number() Over( Partition By Id Order By Data ) As ColNum
        From SpeciInfo
        )
    Select Id
        , Min( Case When Num = 1 Then Data End ) As Custom1
        , Min( Case When Num = 2 Then Data End ) As Custom2
        , Min( Case When Num = 3 Then Data End ) As Custom3
        , Min( Case When Num = 4 Then Data End ) As Custom4
        ...
    From NumberedItems
    Group By Id
    

    One serious problem in your original data is that there is no indicator of sequence and thus there is no means for the system to know which item for a given ID should appear in the Custom1 column as opposed to the Custom2 column. In my query above, I arbitrarily ordered by name.

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

Sidebar

Related Questions

I have following data in my table. alt text http://img26.imageshack.us/img26/3746/productfield.png I want to extract
Hi I have following data in the table: ID-----startDate----endDate 5549 2008-05-01 4712-12-31 5567 2008-04-17
I have the following data set that I am trying to plot with ggplot2,
I have a sql table which have the following data, Id City Country ---
I have the following data in my table: id invoice_id date ammount 1 1
I have the following data in a database table, Columns : Date, Hour(from 1
I have following data in a mysql table, +------------+--------+--------+ | date | inQty |
I have the following data in a table TABLE1 DOCUMENT ------ FIELD1 12345 23456
I have the following data in a table. Name LeftId RightId ------------------------------------------ Cat 1
I have the following data in the table SALES: BillItemID CustID BillDate Item BillAmt

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.