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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:44:36+00:00 2026-06-13T17:44:36+00:00

I have the following table with the following sample data ID Language Question SubQuestion

  • 0

I have the following table with the following sample data

ID  Language       Question       SubQuestion     SubSubQuestion    TotalCount  TotalPercent
3      E               9               0                1             88527            73%
3      E               9               0                2             19684            16%
3      E               9               0                3             12960            11%
3      E               9               0                9              933              1%

I want all in one row like this

    ID  Language        TotalCount901   TotalPercent901     TotalCount902   TotalPercent902 TotalCount903   TotalPercent903
     3       E            88527           73%                 19684             16%              12960              11%

I’ve tired using the pivot command, but it dosnt to work for me.

  • 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-13T17:44:38+00:00Added an answer on June 13, 2026 at 5:44 pm

    I made a few assumptions based on your column names, but it looks like you want to use something similar to this. This applies both an UNPIVOT and then a PIVOT to get the values in the columns you requested:

    select *
    from
    (
      select id,
        language,
        col + cast(QUESTION as varchar(10))
          +cast(subquestion as varchar(10))
          +cast(SubSubQuestion as varchar(10)) col,
        value
      from
      (
        select id, language,
          cast(TotalCount as varchar(10)) TotalCount, 
          totalPercent,
          question, subquestion, SubSubQuestion
        from yourtable
      ) usrc
      unpivot
      (
        value 
        for col in (totalcount, totalpercent)
      ) un
    ) srcpiv
    pivot
    (
      max(value)
      for col in ([TotalCount901], [totalPercent901], 
                  [TotalCount902], [totalPercent902], 
                  [TotalCount903], [totalPercent903],
                  [TotalCount909], [totalPercent909])
    ) p
    

    See SQL Fiddle with Demo

    Note: when performing the UNPIVOT the columns need to be of the same datatype. If they are not, then you will need to convert/cast to get the datatypes the same.

    If you have an unknown number of values to transform, you can use dynamic sql:

    DECLARE @query  AS NVARCHAR(MAX),
        @colsPivot as  NVARCHAR(MAX)
    
    
    select @colsPivot 
      = STUFF((SELECT  ',' 
                 + QUOTENAME(c.name +
                            cast(QUESTION as varchar(10))
                            +cast(subquestion as varchar(10))
                            +cast(SubSubQuestion as varchar(10)))
               from yourtable t
               cross apply sys.columns as C
               where C.object_id = object_id('yourtable') and
                  C.name in ('TotalCount', 'TotalPercent')
               group by c.name, t.question, t.subquestion, t.subsubquestion
               order by t.SubSubQuestion
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    
    set @query = 'select *
                  from
                  (
                    select id,
                      language,
                      col + cast(QUESTION as varchar(10))
                        +cast(subquestion as varchar(10))
                        +cast(SubSubQuestion as varchar(10)) col,
                      value
                    from
                    (
                      select id, language,
                        cast(TotalCount as varchar(10)) TotalCount, 
                        totalPercent,
                        question, subquestion, SubSubQuestion
                      from yourtable
                    ) usrc
                    unpivot
                    (
                      value 
                      for col in (totalcount, totalpercent)
                    ) un
                  ) srcpiv
                pivot 
                (
                    max(value)
                    for col in (' + @colsPivot + ')
                ) p '
    
    execute(@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 have the following sample data for a particular column symbol for sample table.
I have a log table with the following format (and sample data): date |
I have a MySQL table with following data (sample); UserId - DeviceId - StartDate
I have the following data.frame: sample <- data.frame(day=c(1,2,5,10,12,12,14)) sample.table <- as.data.frame(table(sample$day)) Now what I'd
i have the following mysql tables with some sample data. 1) table: ai_shipment +----+---------------------+---------------------+------------------+
1I have the following two tables (sample data) and need to be able to
I have the following sample of data to insert into tables(from parent to child,
I have a sample file like the following: CREATE GLOBAL TEMPORARY TABLE tt_temp_user_11 (
I have the following mysql table called pics, with the following fields and sample
consider the following table create table sample(id, name, numeric, qno, ans1,ans2,ans3) sample data 1,

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.