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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:13:52+00:00 2026-06-12T00:13:52+00:00

I believe I will have to use both a Pivot and a UnPivot to

  • 0

I believe I will have to use both a Pivot and a UnPivot to get the data that I want.

My current data looks like this:

var_date    var_attribute   var_out_of_ad   var_per_ad
9/4/2012    Aux1            0               0
9/5/2012    Aux1            0               0
9/4/2012    Aux2            1680            0.733333333
9/5/2012    Aux2            1680            0.733333333
9/4/2012    Aux3            1500            0.791666667
9/5/2012    Aux3            1500            0.791666667
9/4/2012    Aux4            0               0
9/5/2012    Aux4            0               0
9/4/2012    Aux5            0               0
9/5/2012    Aux5            0               0
9/4/2012    Aux6            26640           0.766929134
9/5/2012    Aux6            26640           0.766929134
9/4/2012    Aux7            28800           0
9/5/2012    Aux7            28800           0
9/4/2012    Aux8            3600            0
9/5/2012    Aux8            3600            0

I want to be able to break apart by Aux Code (which will by dynamic) the columns var_out_of_ad and var_per_ad so I would like my data to look like this:

var_date  Aux1_per   Aux1_out   Aux2_per   Aux2_out ...
9/4/2012  0          0           .733      1680
9/5/2012  0          0           .733      1680

I have put the testing data into a sql fiddle: http://sqlfiddle.com/#!3/7f06d/1 as well as all of the work I have done with the dynamic pivot.

Can someone please guide me as to what to do next? I am lost.

Thank You

  • 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-12T00:13:53+00:00Added an answer on June 12, 2026 at 12:13 am

    Since you are trying to PIVOT two columns of data, you need to UNPIVOT first, then apply a PIVOT:

    DECLARE @colsPivot AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @colsPivot = Stuff((Select distinct ',' 
                               + QuoteName([var_attribute] + x.type) 
                        from mytable
                        cross apply
                        (
                          select '_per' type
                          union all
                          select '_out' type
                        ) x
                FOR XML Path(''), Type
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    
    set @query = 'select var_date, ' + @colsPivot + '
                  from
                  (
                    select var_date, 
                      val,
                      case col when ''var_out_of_ad'' 
                                  then VAR_ATTRIBUTE + ''_out''  
                              when ''var_per_ad'' 
                                  then VAR_ATTRIBUTE + ''_per'' end var_attribute 
                    from
                    (
                      select var_date, var_attribute,
                          cast(var_out_of_ad as float) var_out_of_ad,
                          var_per_ad
                      from mytable
                    ) x
                    unpivot
                    (
                      val
                      for col in (var_out_of_ad, var_per_ad)
                    ) u
                  ) x1
                  pivot
                  (
                    max(val)
                    for var_attribute in ('+@colsPivot +')
                  )p'
    
    exec(@query)
    

    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've got a queue (actually 3 different queues but I don't believe that will
It is generally accepted (I believe!) that a lock will force any values from
I'm developing a mobile app that will be available on both android and ios.
I want to build a web application (SaaS) that can work both in Online
Suppose I have ViewModel like public class AnotherViewModel { public string Name { get;
I believe that when my application starts nothing is focused. At least it looks
I have a table that tracks tags users create. The important columns for this
Not used set_intersection before, but I believe it will work with maps. I wrote
I believe I've set this up correctly. Can somebody see anything wrong with this
I believe that the rallytagpicker is definitely a useful component. However, I do not

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.