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

  • Home
  • SEARCH
  • 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 8746569
In Process

The Archive Base Latest Questions

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

This is the data: id name period data1 data2 =================================================== 111 name1 monthly aaaaa

  • 0

This is the data:

id    name     period      data1    data2
===================================================
111   name1    monthly     aaaaa    bbbbb
111   name1    quaterly    ccccc    ddddd
111   name1    halfYearly  eeeee    fffff
111   name1    annually    ggggg    hhhhh

I want query which fetch data in a single row like

id    name     monthlYdata1    monthlYdata2      quaterlydata1    quaterlydata2      halfYearlydata1    halfYearlydata2      annuallydata1    annuallydata2
==========================================================================================================================================================
111   name1    aaaaa           bbbbb             ccccc            ddddd              eeeee              fffff                ggggg            hhhhh
  • 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-13T12:12:56+00:00Added an answer on June 13, 2026 at 12:12 pm

    You did not specify what RDBMS you are using but, this will work in all of them:

    select id,
      name,
      max(case when period = 'monthly' then data1 end) as MonthlyData1,
      max(case when period = 'monthly' then data2 end) as MonthlyData2,
      max(case when period = 'quaterly' then data1 end) as quarterlyData1,
      max(case when period = 'quaterly' then data2 end) as quarterlyData2,
      max(case when period = 'halfYearly' then data1 end) as halfYearlyData1,
      max(case when period = 'halfYearly' then data2 end) as halfYearlyData2,
      max(case when period = 'annually' then data1 end) as annuallyData1,
      max(case when period = 'annually' then data2 end) as annuallyData2
    from yourtable
    group by id, name
    

    See SQL Fiddle with Demo

    If you are using an RDBMS that has a PIVOT function, then you can do the following which uses both an UNPIVOT and PIVOT to produce the results. As Andriy M pointed out the UNPIVOT is assuming that the datatype for both data1 and data2 are the same types, if not, then a conversion would need to take place to UNPIVOT the data:

    Oracle 11g:

    select *
    from
    (
      select id, name, value, 
        period||data new_col
      from yourtable
      unpivot
      (
        value for data in (data1, data2)
      ) u
    ) x
    pivot
    (
      max(value)
      for new_col in ('monthlyDATA1', 'monthlyDATA2',
                      'quaterlyDATA1', 'quaterlyDATA2',
                      'halfYearlyDATA1', 'halfYearlyDATA2',
                      'annuallyDATA1', 'annuallyDATA2')
    ) p
    

    See SQL Fiddle with Demo

    SQL Server:

    select *
    from
    (
      select id, name, value, 
        period+data new_col
      from yourtable
      unpivot
      (
        value for data in (data1, data2)
      ) u
    ) x
    pivot
    (
      max(value)
      for new_col in ('monthlyDATA1', 'monthlyDATA2',
                      'quaterlyDATA1', 'quaterlyDATA2',
                      'halfYearlyDATA1', 'halfYearlyDATA2',
                      'annuallyDATA1', 'annuallyDATA2')
    ) p
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

between saving a key name like this for(var key in this.data) { var key_name
I have this data in a table, for instance, id name parent parent_id 1
I have a column of name, varchar(200). In this column data can be filled
I have a list of this class: public class Data { public string name
I have a matrix of objects that contains data in this form: name A,2,name
My data in Oracle is something like this NAME | DEP_VALUE | ID_DEP Amy
Because data from file look like this: line 1 is name (first last), next
I have the following query: -- Compare current period to historical data select Name
Iam trying to return data ( the stored exercises information : Name , period
i have this data | car_name | year | price | Honda | 2011

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.