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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:05:56+00:00 2026-06-16T00:05:56+00:00

I am using sql server 2008 and I want to cross tab this table

  • 0

I am using sql server 2008 and I want to cross tab this table

Month   Affec   KPI     Total   KPI_%   Out     rep_in_10  ftm
Jan-11  30565   34623   42003   82.4    7380    7003       5024
Jan-12  20955   25915   27857   93      1942    4754       3518
Feb-11  27754   27757   36483   76.1    8726    5648       4189
Feb-12  19513   25188   26962   93.4    1774    5768       4185
Mar-11  22838   23758   29951   79.3    6193    4394       3282
Mar-12  18778   25098   26177   95.9    1079    5784       4105
Apr-11  20235   21950   25917   84.7    3967    3895       2967

to

            Jan-11  Jan-12  Feb-11  Feb-12  Mar-11  Apr-11
Affec       30565   
KPI         34623   
Total       42003   
KPI_%       82.4         
Out         7380    
rep_in_10   7003
  • 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-16T00:05:57+00:00Added an answer on June 16, 2026 at 12:05 am

    For this type of data transformation, you will need to use the UNPIVOT function and then apply the PIVOT function in SQL Server.

    There are two ways to perform this, either hard-coding the values with a static version of using dynamic sql to generate the values as run-time.

    Static Version:

    The UNPIVOT piece of this takes the data from your multiple columns and transforms it into two rows. Note, the one thing to keep in mind with unpivot is that the datatypes must be the same. So you might have to perform a data type conversion on the data.:

    select [Month], value, col
    from
    (
      select DateName(month,[Month]) +'-'+Cast(datepart(year, [month]) as varchar(4)) Month,
        [Affec], [KPI], [Total], [KPI_%], [Out], [rep_in_10], [ftm]
      from yourtable
    ) src
    unpivot
    (
      value
      for col in ([Affec], [KPI], [Total], [KPI_%], [Out], [rep_in_10], [ftm])
    ) unpiv
    

    See SQL Fiddle with Demo

    Result:

    |         MONTH | VALUE |       COL |
    -------------------------------------
    |  January-2011 | 30565 |     Affec |
    |  January-2011 | 34623 |       KPI |
    |  January-2011 | 42003 |     Total |
    |  January-2011 |  82.4 |     KPI_% |
    |  January-2011 |  7380 |       Out |  ---etc
    

    Then you apply the PIVOT to the months:

    select *
    from
    (
      select [Month], value, col
      from
      (
        select DateName(month,[Month]) +'-'+Cast(datepart(year, [month]) as varchar(4)) Month,
          [Affec], [KPI], [Total], [KPI_%], [Out], [rep_in_10], [ftm]
        from yourtable
      ) src
      unpivot
      (
        value
        for col in ([Affec], [KPI], [Total], [KPI_%], [Out], [rep_in_10], [ftm])
      ) unpiv
    ) src
    pivot
    (
      max(value)
      for month in ([January-2011], [February-2011], [March-2011],
                 [April-2011], [January-2012], [February-2012], [March-2012])
    ) piv
    

    See SQL Fiddle with Demo

    Result:

    |       COL | JANUARY-2011 | FEBRUARY-2011 | MARCH-2011 | APRIL-2011 | JANUARY-2012 | FEBRUARY-2012 | MARCH-2012 |
    ------------------------------------------------------------------------------------------------------------------
    |     Affec |        30565 |         27754 |      22838 |      20235 |        20955 |         19513 |      18778 |
    |       ftm |         5024 |          4189 |       3282 |       2967 |         3518 |          4185 |       4105 |
    |       KPI |        34623 |         27757 |      23758 |      21950 |        25915 |         25188 |      25098 |
    |     KPI_% |         82.4 |          76.1 |       79.3 |       84.7 |           93 |          93.4 |       95.9 |
    |       Out |         7380 |          8726 |       6193 |       3967 |         1942 |          1774 |       1079 |
    | rep_in_10 |         7003 |          5648 |       4394 |       3895 |         4754 |          5768 |       5784 |
    |     Total |        42003 |         36483 |      29951 |      25917 |        27857 |         26962 |      26177 |
    

    Dynamic Version:

    The above works great, if you have a known number of values but it your values are unknown then you will want to use dynamic sql. I am going to guess that you will want a dynamic version since you will have a unknown number of dates:

    DECLARE @colsUnpivot AS NVARCHAR(MAX),
        @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @colsUnpivot = STUFF((SELECT DISTINCT ',' 
                          + quotename(c.name)
                        from sys.columns as C
                       where C.object_id = object_id('yourtable') and
                             C.name not in ('Month')
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    select @cols = STUFF((SELECT ',' + QUOTENAME(DateName(month,[Month]) +'-'+Cast(datepart(year, [month]) as varchar(4))) 
                        from yourtable
                        group by [Month]
                        order by [Month]
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    set @query = 'SELECT col,' + @cols + ' from 
                 (
                   select [Month], value, col
                  from
                  (
                    select DateName(month,[Month]) +''-''+Cast(datepart(year, [month]) as varchar(4)) Month,
                      [Affec], [KPI], [Total], [KPI_%], [Out], [rep_in_10], [ftm]
                    from yourtable
                  ) src
                  unpivot
                  (
                    value
                    for col in ('+@colsunpivot+')
                  ) unpiv
                ) x
                pivot
                (
                  max(value)
                  for [Month] in (' + @cols + ')
                ) p '
    
    execute(@query)
    

    See SQL Fiddle with Demo

    The result will be the same as the dynamic version.

    UNION ALL/CASE with aggregate:

    Lastly, if you do not have access to either the UNPIVOT or PIVOT functions, then you can use a UNION ALL to unpivot and an aggregate function with a CASE to pivot the data:

    select col,
      max(case when month='January-2011' then value end) [January-2011],
      max(case when month='February-2011' then value end) [February-2011],
      max(case when month='March-2011' then value end) [March-2011],
      max(case when month='April-2011' then value end) [April-2011],
      max(case when month='January-2012' then value end) [January-2012],
      max(case when month='February-2012' then value end) [February-2012],
      max(case when month='March-2012' then value end) [March-2012]
    from
    (
      select DateName(month,[Month]) +'-'+Cast(datepart(year, [month]) as varchar(4)) Month,
        [Affec] value,
        'Affec' col
      from yourtable
      union all
      select DateName(month,[Month]) +'-'+Cast(datepart(year, [month]) as varchar(4)) Month,
        [KPI] value,
        'KPI' col
      from yourtable
      union all
      select DateName(month,[Month]) +'-'+Cast(datepart(year, [month]) as varchar(4)) Month,
        [Total] value,
        'Total' col
      from yourtable
      union all
      select DateName(month,[Month]) +'-'+Cast(datepart(year, [month]) as varchar(4)) Month,
        [KPI_%] value,
        'KPI_%' col
      from yourtable
      union all
      select DateName(month,[Month]) +'-'+Cast(datepart(year, [month]) as varchar(4)) Month,
        [rep_in_10] value,
        'rep_in_10' col
      from yourtable
      union all
      select DateName(month,[Month]) +'-'+Cast(datepart(year, [month]) as varchar(4)) Month,
        [ftm] value,
        'ftm' col
      from yourtable
    ) src
    group by col
    

    See SQL Fiddle with Demo

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

Sidebar

Related Questions

I'm using SQL Server 2008. I have a database table that looks like this
I am using SQL Server 2008 Management Studio and have a table I want
I am using SQL Server 2008 and C# ADO.NET to accomplish this. I want
I want to sort a table in SQL Server 2008 by using one column
I am using SQL Server 2008. I have a table like this: UnitId ParentId
I am using SQL Server 2008 R2 and I want to add a non-clustered
I am using SQL Server 2008 Enterprise version. I want to know if I
I want to develop a generic CRM application using SQL Server 2008. The application
I want to write a code to backup my Sql Server 2008 Database using
Using SQL Server 2008, is there a way to allow inserts to a table

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.