Avatar of codeoxygen
codeoxygen
 asked on

How convert each row of table to as columns in sql server

hi,
      I want convert entire row in table as single column , example is show below

QuteNo     || requestNo  ||      cat1    || cat2    || cat3
------------------------------------------------------------------------------------
01                            1                        a               b               c
2                               1                        d               e               f
3                                2                       g                  k              l

Expected Result :

 heading          01    2    3
requestno        1      1    2
  cat1                 a       d     g
  cat2                 b       e    k
    cat3               c         f    i
Microsoft SQL Server

Avatar of undefined
Last Comment
Vitor Montalvão

8/22/2022 - Mon
Vitor Montalvão

You'll need to use PIVOT but usually you'll need to know the field values otherwise you'll need to do it dynamically that it's more complex.
codeoxygen

ASKER
Vitor sir  i used it but it shows error  so please how to use it can u explain
Vitor Montalvão

Can you post your actual query and the error returned?
Your help has saved me hundreds of hours of internet surfing.
fblack61
codeoxygen

ASKER
vitor sir above i designed the expected result  ,that result query i need can u  write the code
Vitor Montalvão

If you post your code and the error I'll be happy to show you how and where to change it so it can work.
codeoxygen

ASKER
Table structure and data are given in file. Please find attachment
Query.sql
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Vitor Montalvão

You query returns error:
Msg 207, Level 16, State 1, Line 73
Invalid column name 'Insuranceprovider'.
Msg 207, Level 16, State 1, Line 74
Invalid column name 'Insurancetype'.
codeoxygen

ASKER
codeoxygen

ASKER
table structure and data are given in file .please find attachment
Query.sql
Query.sql
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
Vitor Montalvão

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.