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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:24:56+00:00 2026-06-14T23:24:56+00:00

A_first B_first C_first A_second B_second C_second A_third B_third C_third 638 450 188 638 439

  • 0
A_first B_first C_first  A_second  B_second  C_second A_third  B_third  C_third
638      450     188       638       439       187      546      256      789

I have a table like that with only one row. How can I pivot this row so I get A B C as columns and a row for each _first, _second, _third?

I’m trying to wrap my head around the pivot/unpivot command in sql

A          B      C     
638      450     188     
638      439     187     
546      256     789

Using SQL Server 2008 so I have the pivot/unpivot command. More specifically SSMS tells me the database I’m connected to is 9.0 SP3

  • 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-14T23:24:58+00:00Added an answer on June 14, 2026 at 11:24 pm

    You did not specify what RDBMS you are using so here are two solutions. If you have an UNPIVOT function and then a PIVOT:

    select *
    from
    (
      select value, left(fields, 1) col,
        substring(fields, 3, len(fields) -2) row
      from yourtable
      unpivot
      (
        value
        for fields in(A_first, B_first, C_first,
                      A_second, B_second, C_second,
                      A_third, B_third, C_third)
      ) unpiv
    ) src
    pivot
    (
      max(value)
      for col in ([A], [B], [C])
    ) piv
    

    See SQL Fiddle with Demo

    If you do not have an UNPIVOT and PIVOT function, then you can use UNION ALL:

    select
      max(case when col = 'A' then value end) A,
      max(case when col = 'B' then value end) B,
      max(case when col = 'C' then value end) C
    from
    (
      select value, left(fields, 1) col, substring(fields, 3, len(fields) -2) row
      from
      (
        select A_first value, 'A_first' fields
        from yourtable
        union all
        select B_first value, 'B_first' fields
        from yourtable
        union all
        select C_first value, 'C_first' fields
        from yourtable
        union all
        select A_second value, 'A_second' fields
        from yourtable
        union all
        select B_second value, 'B_second' fields
        from yourtable
        union all
        select C_second value, 'C_second' fields
        from yourtable
        union all
        select A_third value, 'A_third' fields
        from yourtable
        union all
        select B_third value, 'B_third' fields
        from yourtable
        union all
        select C_third value, 'C_third' fields
        from yourtable
      ) unpiv
    ) src
    group by row
    

    See SQL Fiddle with Demo

    Both will produce the same result:

    |   A |   B |   C |
    -------------------
    | 638 | 450 | 188 |
    | 638 | 439 | 187 |
    | 546 | 256 | 789 |
    

    Note, in order to perform this task the data types must be the same on each column. If they are not then you will need to convert the data first prior to the UNPIVOT.

    This answer makes a few assumptions:

    1. That the existing column will always start with the new column value (A,
      B or C, etc)
    2. That the existing column will always start with two characters
      before the row number.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a following oracle query: SELECT a.USER_ID, c.first_name, c.last_name, TO_CHAR( b.logon_date, 'MM/DD/YYYY HH:MI:SS
Via Function: myfunc Parameters: a - First parameter b - First parameter I can
E.g. vector<string> a; vector<string> b; a.push_back(first); b=a; Would it be optimised somehow as vector<string>
First Model class A(db.Model): number = db.IntgerProperty() second Model class B(db.Model): name = db.StringProperty()
First code : public static int pitagoras(int a, int b) { return (int) Math.sqrt(a*a
First, a little background. I have strings that resemble the following: ((Foo.Bar.StartsWith(A)) && (Foo.Bar.EndsWith(B)))
I have 2 activity: A, B. The first is an TabActivity and the second
I'm a first time programmer, first time StackOverflow user, studying from Allen Downey's free
I'm working on a first symfony project, i am using the sfWidgetFormTextareaTinyMCE widget for
We are building a first person shooter and we want to give out 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.