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

The Archive Base Latest Questions

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

I would like to ask for help.. I have this table user_id | Metric_1

  • 0

I would like to ask for help.. I have this table

user_id  | Metric_1 | Metric_2 | Metric_3
------------------------------------------
1        | Jan      | 10       | yes
2        | Feb      | 10       | No
3        | Mar      | 20       | No
4        | Apr      | 30       | No
5        | May      | 40       | yes
6        | Jun      | 50       | No
7        | Jul      | 60       | No
8        | Aug      | 70       | yes
9        | Sep      | 80       | No
10       | Oct      | 90       | yes
11       | Nov      | 10       | No
12       | Dec      | 20       | No

I would like to get this result in a query, I’m using postgres tablefunc crosstab but I can get it to have this result… hope you can help me

| January | February | March | April | May
 ---------------------------------------------------
| 10      | 10       | 20    |30     |40
| yes     | NO       | No    |yes    |No
  • 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-18T00:42:03+00:00Added an answer on June 18, 2026 at 12:42 am

    For this type of transformation you can unpivot and then pivot the data. For the unpivot, you can use a UNION ALL query and then apply an aggregate function with a CASE expression to pivot the data:

    select 
      max(case when metric_1 ='Jan' then value end) as Jan,
      max(case when metric_1 ='Feb' then value end) as Feb,
      max(case when metric_1 ='Mar' then value end) as Mar,
      max(case when metric_1 ='Apr' then value end) as Apr,
      max(case when metric_1 ='May' then value end) as May,
      max(case when metric_1 ='Jun' then value end) as Jun,
      max(case when metric_1 ='Jul' then value end) as Jul,
      max(case when metric_1 ='Aug' then value end) as Aug,
      max(case when metric_1 ='Sep' then value end) as Sep,
      max(case when metric_1 ='Oct' then value end) as Oct,
      max(case when metric_1 ='Nov' then value end) as Nov,
      max(case when metric_1 ='Dec' then value end) as "Dec"
    from
    (
      select user_id, metric_1, cast(metric_2 as varchar(10)) as value, 'Metric2' as col
      from yourtable
      union all
      select user_id, metric_1, metric_3 as value, 'Metric3' as col
      from yourtable
    ) src
    group by col
    order by col
    

    See SQL Fiddle with Demo

    The result is:

    | JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC |
    -------------------------------------------------------------------------
    |  10 |  10 |  20 |  30 |  40 |  50 |  60 |  70 |  80 |  90 |  10 |  20 |
    | yes |  No |  No |  No | yes |  No |  No | yes |  No | yes |  No |  No |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to ask for help on how to achieve this. I have
I would like to ask again the help of the Python experts in this
I would like to ask for help regarding datasets and Crystal Reports. I have
I would like to ask some help. This is not homework but it is
Good morning! I would like to ask some help from you guys on how
I'm still fairly new to mysql and would like to ask for some help.
I would like to ask you: Imagine that you have a site and there
I would like to ask such question, I have XML xsd`s, which generate beans
I would like to ask you about regular expressions preg_match have outlined below. I
I would like to ask some help regarding sql-mvn-plugin . According to the documentation

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.