-4

I have a table that looks like this

enter image description here

and I want it like below, how should I do it in sql? If I use GROUP BY I am unable to use max, min functions on strings

result I want.
enter image description here

Morgan Thrapp
  • 9,748
  • 3
  • 46
  • 67
Nisha Nethani
  • 109
  • 1
  • 1
  • 11
  • Check pivoting concept: https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx – Hozikimaru Oct 09 '15 at 18:06
  • 1
    Possible duplicate of [Efficiently convert rows to columns in sql server](http://stackoverflow.com/questions/15745042/efficiently-convert-rows-to-columns-in-sql-server) – Tab Alleman Oct 09 '15 at 18:21

1 Answers1

1
SELECT *
FROM TableName t
 PIVOT (MAX(Name)
        FOR EMPLOYEETYPE
        IN (ENGINEER, MANAGER, TECHNICIAN)
       )p

Since you have mentioned, you have joins and some other stuff in your actual query all you need to do is

SELECT * FROM 
(
  /* 
                  Your Query here  
    just make sure it is only returning the columns shown in your question  
*/
)t
     PIVOT (MAX(Name)
            FOR EMPLOYEETYPE
            IN (ENGINEER, MANAGER, TECHNICIAN)
           )p
M.Ali
  • 67,945
  • 13
  • 101
  • 127
  • THE first table is actually a subquery which gets the data from 3 tables by joining them. Adding pivot to it is not working for me – Nisha Nethani Oct 09 '15 at 19:29
  • If a project can have more than 1 person of the same type, you'll need to use row_number in the pivot. Otherwise this will only show max. one person per type per project – James Z Oct 09 '15 at 19:39