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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:54:38+00:00 2026-05-14T03:54:38+00:00

I have a query that returns one row. However, I want to invert the

  • 0

I have a query that returns one row. However, I want to invert the rows and columns, meaning show the rows as columns and columns as rows. I think the best way to do this is to use a pivot table, which I am no expert in.

Here is my simple query:

SELECT Period1, Period2, Period3
FROM GL.Actuals
WHERE Year = 2009 AND Account = '001-4000-50031'

Results (with headers):

Period1, Period2, Period3

612.58, 681.36, 676.42

I would like for the results to look like this:

Desired Results:

Period, Amount

Jan, 612.58

Feb, 681.36

Mar, 676.42

This is a simple example, but what I’m really after is a bit more comlex than this. I realize I could produce theses results by using several SELECT commands instead. I’m just hoping someone can shine some light on how to accomplish this with a Pivot Table or if there is yet a better way.

  • 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-14T03:54:39+00:00Added an answer on May 14, 2026 at 3:54 am

    Try something like this (tested on MSSQL2008):

    DECLARE @Data TABLE(Period1 Decimal(5, 2), Period2 Decimal(5, 2), Period3 Decimal(5, 2))
    INSERT @Data
    VALUES (612.58, 681.36, 676.42)
    
    SELECT Period, Amount
    FROM (SELECT Period1 AS Jan, Period2 AS Feb, Period3 AS Mar FROM @Data) AS D
    UNPIVOT (Amount FOR Period IN (Jan, Feb, Mar)) AS U
    

    Update

    Based on Jeff’s comment, how about this:

    DECLARE @Actuals TABLE(Account INT, [Year] INT, Period1 Decimal(5, 2), Period2 Decimal(5, 2), Period3 Decimal(5, 2))
    INSERT @Actuals VALUES (1, 2010, 612.58, 681.36, 676.42)
    INSERT @Actuals VALUES (1, 2009, 512.58, 581.36, 576.42)
    
    SELECT Account, Period, Amount
    FROM
    (
        SELECT a.Account, a.Period1 AS Jan, a.Period2 AS Feb, a.Period3 AS Mar, a1.Period1 AS Jan1, a1.Period2 AS Feb1, a1.Period3 AS Mar1
        FROM @Actuals AS a
        LEFT OUTER JOIN @Actuals AS a1 ON a.Account = a1.Account AND a1.[Year] = a.[Year] - 1
        WHERE a.[Year] = 2010
    ) AS d
    UNPIVOT (Amount FOR Period IN (Jan, Feb, Mar, Jan1, Feb1, Mar1)) AS u
    

    or, with an explicit year column:

    DECLARE @Actuals TABLE(Account INT, [Year] INT, Period1 Decimal(5, 2), Period2 Decimal(5, 2), Period3 Decimal(5, 2))
    INSERT @Actuals VALUES (1, 2010, 612.58, 681.36, 676.42)
    INSERT @Actuals VALUES (1, 2009, 512.58, 581.36, 576.42)
    
    SELECT Account, [Year], Period, Amount
    FROM
    (
        SELECT a.Account, a.[Year], a.Period1 AS Jan, a.Period2 AS Feb, a.Period3 AS Mar
        FROM @Actuals AS a WHERE a.[Year] IN (2009, 2010)
    ) AS d
    UNPIVOT (Amount FOR Period IN (Jan, Feb, Mar)) AS u
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that always returns one row, with many columns. I would
i have a query that returns rows that i want, e.g. QuestionID QuestionTitle UpVotes
I have a query that I know it should atleast return one row. How
I have a sub query that returns one column, showing as GroupType, I then
I have a query that returns a result set similar to the one below:
i have a query that returns ~50k rows, seems like doctrine put this whole
I have a SQL query that returns two columns - Title and Count. When
I have a query that I am currently getting the ORA-01427: single-row subquery returns
I have a query that returns a string, as well as an escape character
I have a query that returns a lot of data into a CSV file.

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.