Required pivot for below data structured

pivot table
why do employers want potential employees to know how to create pivot tables and charts?
normalize data for excel pivot table
pivot table not picking up data
pivot table requirements
how to find where a pivot table is pulling data from
pivot table rows not nested
types of pivot tables

i have data in below format.

ID   question    answer
1    who         A
1    where       B
1    when        C
1    how         D

i required output as below

id   who   where   when   how
1    A     B       C      D

You can try this using dynamic pivot query.

create table #temp (ID int, question varchar(20), answer char(1))
insert into #temp values
(1,    'who',         'A'),
(1,    'where',       'B'),
(1,    'when',        'C'),
(1,    'how',         'D')

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX);

SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.question) 
            FROM #temp c
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT Id, ' + @cols + ' from 
            (
                select Id
                    ,question
                    , answer
                from #temp
           ) x
            pivot 
            (
                 max(answer)
                for question in (' + @cols + ')
            ) p '


execute(@query)

Live Demo

How to Structure Source Data for a Pivot Table , The correct vs. incorrect structure for pivot table source data. Your source data should be setup in a table layout similar to the table in the image below. So understanding why you need it in the correct format will save you� In order to create a Pivot Table, you need a structure as shown on the right hand side. Each column contains one data type. As you have 4 different data fields (product, type (revenue or cost), year and the values) you need 4 columns, one for each data type.

use conditional aggregation

  select id, 
  max(case when question='who' then answer end) as "who",
  max(case when question='where' then answer end) as "where",
  max(case when question='when' then answer end) as "when",
  max(case when question='how' then answer end) as "how"
   from table_name
  group by id

How to Fix Excel Pivot Table Source Data, If a pivot table's source data is set up incorrectly, it will be difficult to In some workbooks, the data is set up like the example shown below, with a structure is a poor layout to use as source data for an Excel pivot table. To see an annual total, a calculated field would be required, to sum all 12 months. The first step to creating a pivot table is setting up your data in the correct table structure or format. This is the source data you will use when creating a pivot table. Your source data should be setup in a table layout similar to the table in the image below. The following is a list of components of a data table.

You may try this.

    DECLARE @column AS VARCHAR(MAX), @query  AS VARCHAR(MAX);

    SET @column = STUFF((SELECT distinct ',' + QUOTENAME(t.question) FROM table t FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'),1,1,'')

    set @query = 'SELECT Id, ' + @column + ' from 
                (
                    select Id, question, answer from table
                ) x
                pivot 
                ( max(answer) for question in (' + @column + ')
                ) p '


    exec (@query) 

Pivotable: How to Prepare Data for Creating Pivot Tables in Excel , Insert a Pivot Table with 3 steps: Select the data, click on 'PivotTable' on the ' Insert' ribbon and pivot, table, pivotable, structure, create, requirements, excel It copies all values into one column underneath each other. Very cool Jeff! I like the concept and similarity to structured reference behavior with the named ranges. I think the CUBE functions are probably the closest thing you're going to get to a built-in functions in Excel. Of course the CUBE functions don't work with standard pivot tables. You will need a data model like PowerPivot.

Pivot Table Tips, Quickly learn more than 20 things you should know about Excel Pivot Tables. If you have well-structured source data, you can create a pivot table in less than a minute. Keep things simple, and focus on the questions you need to answer. Changing the Data Source for an Existing Pivot Table. If you have an existing pivot table that uses a regular range as the source, you can change it to use a Table as the source. Here's an animated screencast with the instructions below: Go to the source data range and Insert a Table (Insert tab on the Ribbon > Table).

How to get data in the right format with pivot tables, How to transform data from the long to the wide format. that I found at Our World in Data. You can create charts like the one below for free at datawrapper. de. In our line chart up there, the structure is the following: variable 1: Note how we first need to select the data we want to include in the pivot table. If you have more than one field in any of the PivotTable areas, then the PivotTable layout depends on the order you place the fields in that area. This is called the Nesting Order. If you know how your data is structured, you can place the fields in the required order. If you are not sure about the

How to Create a Pivot Table in Excel: Pivot Tables Explained, Throughout this guide, you need a data set to practice. I've included one for create a Pivot Table. Follow the 6 steps to a Pivot Table outlined below. Use the raw sales data with the following 'field' structure: This lists all the� If you use an Excel Table for the source data of your pivot table, you get a very nice benefit: your data range becomes "dynamic". A dynamic range will automatically expand and shrink the table as you add or remove data, so won't have to worry that the pivot table is missing the latest data. When you use a Table for your pivot table, the pivot

Comments
  • You can check and accept my answer if it helped you.
  • Hello Suraj, This query is working for this example but i have many records for single id in that case i am getting below error. The number of elements in the select list exceeds the maximum allowed number of 4096 elements.
  • I think in that case you need to provide some filter condition to limit the records.
  • i have so many number of Questions for single id, i.e more than 1000. How can i get column names dynamically?
  • Use for all question this process or change your db structure
  • @SurajKumar hahah i missed a double qote thanks but for this you given down vote i think you remove now
  • @ZaynulAbadinTuhin Removed down voting.