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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:06:47+00:00 2026-06-08T06:06:47+00:00

I am trying to pivot a table and am having a bit of difficulty.

  • 0

I am trying to pivot a table and am having a bit of difficulty. My table schema looks like this:

ID  W_C    W_P      A_C   A_P
3   257    251342   217   206078
4   443    109023   332   87437
6   17     9985     32    13515

What I am trying to accomplish is something to the effect of:

        3       4       6
w_c     257     443     17
w_p     251342  109023  9985
a_c     217     332     87437
a_p     206078  87437   13515

I do not need to display the w_c, w_p, a_c, or a_p I just was using it as a point of reference.

I think there may be a way of doing it with pivot/unpivot but I am not very familiar with them, and what I have read isn’t helping me.

I tried doing something using CTE’s but I believe it is overly complicated and is just bad practice:

;with [3_Data] as (
    Select
        1 as [common_key3]
        ,max(Case when [id] = 3 then [w_c] else 0 end) as [3_Records]
    From [example]
), [4_data] as (
Select
        1 as [common_key4]
        ,max(Case when [id] = 4 then [w_c] else 0 end) as [4_Records]
    From [example]
), [6_data] as (
    Select
        1 as [common_key6]
        ,max(Case when [id] = 6 then [w_c] else 0 end) as [6_Records]
    From [example]
)
Select [3_Records], [4_Records], [6_Records]
From [3_Data]
    inner join [4_data] on [common_key3] = [common_key4]
    inner join [6_data] on [common_key3] = [common_key6]

Sql fiddle with table already created: http://sqlfiddle.com/#!3/02ef2/6

  • 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-08T06:06:49+00:00Added an answer on June 8, 2026 at 6:06 am

    You can use an UNPIVOT and then a PIVOT to get the result you want (See SQL Fiddle with Demo):

    select col, [3], [4], [6]
    from
    (
      select id, value, col
      from
      (
        select id, w_c, w_p, a_c, a_p
        from t
      ) x
      unpivot
      (
        value
        for col in (w_c, w_p, a_c, a_p)
      )u
    ) x1
    pivot
    (
      sum(value)
      for id in ([3], [4], [6])
    ) p
    

    In this query you will first, UNPIVOT which will allow you easier access to the data to transform. You can use whatever names you want for the value and col fields, I just chose that for this example. Once the data is unpivoted, you can then apply a PIVOT to get the final product.

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

Sidebar

Related Questions

Trying to figure out how I can do this properly. The print_r looks like
I am trying to create group totals in this pivot table create table test5(city
Whilst trying to pivot a sql table I came across this post Here .
I'm trying to remove relationships from a pivot table with ORM's remove method. This
This seems like a simple Pivot Table to learn with. I would like to
I am trying to create a pivot table type view in postgresql and am
I am trying to make a pivot table with rollup and a case statement
I'm trying to query a pivot table with Kohana's ORM and I'm wondering if
I am facing a problem trying to perform a pivot on the table. A
I'm trying to pivot a table so I can output the data as a

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.