Skip to content
Advertisement

SQL Pivot of string

i have the following data in SQL 2017

however i am new to pivot in sql and i tried different ways and not successful with the result i want it. below is the data i want, can someone please help to provide to achieve this.

i tried with following query

Advertisement

Answer

Hi and thanks for updating.

I created a temporary table named #_tmp and loaded it with all the data that you had in those screenshots.

Input:

Output:

Anyway, here’s the pivot. It uses a Common Table Expression to put a row_number() on each description and value.

Output:

Note that if we just run the CTE:

We get the following. You can see the alternating rn values to indicate groupings when pivoted. That helps keep things in order for our output.

Output:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement