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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:06:57+00:00 2026-06-17T22:06:57+00:00

I have a table that is rather wide that I would like to convert

  • 0

I have a table that is rather wide that I would like to convert to tall. The data currently resides like this:

VEND   YEAR   I1_DOLS   I1_QTY   I2_DOLS   I2_QTY   I3_DOLS   I3_QTY ...
1234   2011   101587    508      203345    334      105938    257
1234   2012   257843    587      235883    247      178475    456
1011   2010   584737    432      587274    356      175737    563
1011   2011   517774    356      483858    456      481785    354

I would like to convert this to a table that looks like this:

VEND   YEAR   MONTH   DOLS     QTY
1234   2011   1       101587   508
1234   2011   2       203345   334
1234   2011   3       105938   257
1234   2012   1       257843   587
1234   2012   2       235883   247
.
.
.

I assume that a PIVOT is what I need, but I can’t seem to figure this out.

  • 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-17T22:06:59+00:00Added an answer on June 17, 2026 at 10:06 pm

    You can unpivot the data using CROSS APPLY (VALUES). Here is an article to explain how this is done:

    http://www.sqlservercentral.com/articles/CROSS+APPLY+VALUES+UNPIVOT/91234/

    Basically the code is:

    SELECT vend,
      year,
      month,
      dols, 
      qty
    FROM YourTable t
    CROSS APPLY 
    (
        VALUES
            (1, I1_DOLS, I1_QTY),
            (2, I2_DOLS, I2_QTY),
            (3, I3_DOLS, I3_QTY)
    ) x (month, dols, qty);
    

    See SQL Fiddle with Demo

    Or you could use a UNION ALL query:

    select vend, year, 1 month, [I1_DOLS] Dols, [I1_QTY] Qty
    from yourtable
    union all
    select vend, year, 2 month, [I2_DOLS] Dols, [I2_QTY] Qty
    from yourtable
    union all
    select vend, year, 3 month, [I3_DOLS] Dols, [I3_QTY] Qty
    from yourtable
    

    See SQL Fiddle with Demo

    Or you can even apply both the UNPIVOT and the PIVOT function to transform the data:

    select *
    from
    (
      select vend,
        year,
        replace(replace(replace(col, 'I', ''), '_Dols', ''), '_Qty', '') month,
        case when col like '%Dols%' then 'dols' else 'qty' end col_name,
        value
      from 
      (
        select vend, year, [I1_DOLS], [I1_QTY], [I2_DOLS], [I2_QTY], [I3_DOLS], [I3_QTY]
        from yourtable
      ) src
      unpivot
      (
        value
        for col in ([I1_DOLS], [I1_QTY], [I2_DOLS], [I2_QTY], [I3_DOLS], [I3_QTY])
      ) un
    ) unp
    pivot
    (
      max(value)
      for col_name in (dols, qty)
    ) piv
    

    See SQL Fiddle with Demo.

    All three will give the same result:

    | VEND | YEAR | MONTH |   DOLS | QTY |
    --------------------------------------
    | 1234 | 2011 |     1 | 101587 | 508 |
    | 1234 | 2011 |     2 | 203345 | 334 |
    | 1234 | 2011 |     3 | 105938 | 257 |
    | 1234 | 2012 |     1 | 257843 | 587 |
    | 1234 | 2012 |     2 | 235883 | 247 |
    | 1234 | 2012 |     3 | 178475 | 456 |
    | 1011 | 2010 |     1 | 584737 | 432 |
    | 1011 | 2010 |     2 | 587274 | 356 |
    | 1011 | 2010 |     3 | 175737 | 563 |
    | 1011 | 2011 |     1 | 517774 | 356 |
    | 1011 | 2011 |     2 | 483858 | 456 |
    | 1011 | 2011 |     3 | 481785 | 354 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that looks like this A B ID1 data 123 ID2
I have a table that looks basically like this: id | redirectid | data
I have a table that looks like this: id value AGA 0.211 AGA 0.433
I have a table that looks like this: CREATE TABLE foobar ( id SERIAL
I have a table that looks like this: ID (pk,int) Col1 (nvarchar) Col2 (nvarchar)
I have a SQLite table that looks like this: CREATE TABLE Cards (id INTEGER
I have a table that looks like this: ID |Name |Parent 1 |A |NULL
I have table that I insert data with following query (from c# code): INSERT
I have a table that stores data that has been entered regarding the amount
Hi guys I have a table like that? <table> <colgroup> <col class=selected> <col> </colgroup>

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.