Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 将数据从列转换为行_Sql_Sql Server_Database - Fatal编程技术网

Sql 将数据从列转换为行

Sql 将数据从列转换为行,sql,sql-server,database,Sql,Sql Server,Database,我有一个数据表,如下所示 ID Full Name Apples Mangoes Grapes 1 Name1 15 19 13 2 Name2 11 16 15 3 Name3 12 18 11 4 Name4 16 20 11 5 Name5 14 19 13 6 Name6 19

我有一个数据表,如下所示

ID  Full Name   Apples  Mangoes Grapes
1     Name1      15       19    13
2     Name2      11       16    15
3     Name3      12       18    11
4     Name4      16       20    11
5     Name5      14       19    13
6     Name6      19       15    18
我想把它变成这样

ID  Full Name   Fruits  Values
1   Name1       Apples  15
1   Name1       Mangoes 19
1   Name1       Grapes  13
2   Name2       Apples  11
2   Name2       Mangoes 16
2   Name2       Grapes  15
3   Name3       Apples  12
3   Name3       Mangoes 18
3   Name3       Grapes  11
4   Name4       Apples  16
4   Name4       Mangoes 20
4   Name4       Grapes  11
5   Name5       Apples  14
5   Name5       Mangoes 19
5   Name5       Grapes  13
6   Name6       Apples  19
6   Name6       Mangoes 15
6   Name6       Grapes  18

是否有可能在Microsoft SQL中执行此操作?

使用
UNPIVOT
执行此操作:

select * from TableName
unpivot([Values] for Fruits in ([Apples], [Mangoes], [Grapes]))u

@jpw,这是禁止的吗?如果已知列,另一种简单方法是: