如何在SQL中将数据从行转换为列

时间:2023-01-28 11:00:51

I have a table that looks like this

我有一张看起来像这样的桌子

如何在SQL中将数据从行转换为列

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

我想要它如下,我应该如何在sql中做到这一点?如果我使用GROUP BY,我无法在字符串上使用max,min函数

result I want.
如何在SQL中将数据从行转换为列

结果我想要。

1 个解决方案

#1


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

#1


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