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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:02:56+00:00 2026-06-18T09:02:56+00:00

I have the following table…let’s call it Table1 TERM PERIOD Average StandardDeviation Minimum +

  • 0

I have the following table…let’s call it Table1

 TERM           PERIOD  Average     StandardDeviation   Minimum
+ 1 Period      2012Q3  0.00417     0.00722             0
+ 120 Periods   2012Q3  0.02951     0.0014              0.028615
+ 20 Periods    2012Q3  0.009898    0.0037              0.007612
+ 40 Periods    2012Q3  0.018255    0.00262             0.016565
+ 1 Period      2012Q4  0.005       0.007               0
+ 120 Periods   2012Q4  0.026       0.001               0.03
+ 20 Periods    2012Q4  0.007       0.003               0.0042
+ 40 Periods    2012Q4  0.018       0.002               0.0165

Is there a way to code this so I can have a table that looks like so:

TERM           PATHS              2012Q3    2012Q4
+ 1 Period     Average            0.00417   0.005
+ 1 Period     StandardDeviation  0.00722   0.007
+ 1 Period     Minimum            0         0

…and so on for each TERM ( + 120 Periods, + 40 Periods, etc).

  • 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-18T09:02:58+00:00Added an answer on June 18, 2026 at 9:02 am

    To produce this result, you will want to use both the UNPIVOT and the PIVOT function. The UNPIVOT takes the columns Average, StandardDeviation and Minimum and converts them into multiple rows. Once you have the rows, then you can apply the PIVOT to the Period values. Your code will be similar to this:

    select term, paths,[2012Q3], [2012Q4]
    from
    (
      select term, period, paths, value
      from
      (
        select term, period, Average, StandardDeviation, Minimum
        from table1
      ) s
      unpivot
      (
        value
        for paths in (Average, StandardDeviation, Minimum)
      ) unpiv
    ) src
    pivot
    (
      max(value)
      for period in ([2012Q3], [2012Q4])
    ) piv
    

    See SQL Fiddle with Demo.

    This can also be done using a UNION ALL to unpivot the data and an aggregate function with a CASE expression to pivot:

    select term,
      paths,
      max(case when period = '2012Q3' then value end) [2012Q3],
      max(case when period = '2012Q4' then value end) [2012Q4]
    from
    (
      select term, period, 'Average' Paths, average value
      from Table1
      union all
      select term, period, 'StandardDeviation' Paths, StandardDeviation
      from Table1
      union all
      select term, period, 'Minimum' Paths, Minimum
      from Table1
    ) src
    group by term,  paths
    

    See SQL Fiddle with Demo.

    The above versions work great if you have a known number of period values. If you don’t then you will want to use dynamic sql to generate the result:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT DISTINCT ',' + QUOTENAME(PERIOD) 
                        from Table1
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query 
        = 'select term, paths, '+@cols+'
           from
           (
             select term, period, paths, value
             from
             (
               select term, period, Average, StandardDeviation, Minimum
               from table1
             ) s
             unpivot
             (
               value
               for paths in (Average, StandardDeviation, Minimum)
             ) unpiv
           ) p
           pivot
           (
              max(value)
              for period in('+@cols+')
           ) piv'
    
    execute(@query)
    

    See SQL Fiddle with Demo.

    All of the versions produce the same result.

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

Sidebar

Related Questions

Let's say I have following table like this <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0
Let's say we have following table. UserId | Message -------|------------- 1 | Hi, have
I have the following table (let's say that its name is AnswerTable: QuestionID Value
Let's assume I have following table in the database: Id ProductId ColorId IsDeleted 1
Let's say I have following table: CREATE TABLE `occurences` ( `object_id` int(10) NOT NULL,
I have following Table with records for ActivityLog: ID, UserId, Category, Created 1 11
I have following table Id | Status | date ------------------- 1 | Onsite |2007
Imagine I have following table: NAME DATE OTHER_CONTANT 'A' '2012-06-05' 'baz' 'A' '2012-06-04' 'bar'
I have following CSS: table tbody tr:last-child td { padding-top: 7px; border-bottom: 0; }
I have the following table drop table names; create table names( name varchar(200), surname

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.