Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.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 Server中的行-列操作_Sql_Sql Server_Tsql - Fatal编程技术网

SQL Server中的行-列操作

SQL Server中的行-列操作,sql,sql-server,tsql,Sql,Sql Server,Tsql,我有一张这样的桌子: 我想从下表中得到一个结果。如何在SQL Server中执行此操作 使用Case时的输出 您可以使用条件聚合: select max(case when col1 = 'a' then e end) as a_e, max(case when col1 = 'a' then f end) as a_f, max(case when col1 = 'a' then g end) as a_g, max(case when col1

我有一张这样的桌子:

我想从下表中得到一个结果。如何在SQL Server中执行此操作

使用Case时的输出


您可以使用条件聚合:

select max(case when col1 = 'a' then e end) as a_e,
       max(case when col1 = 'a' then f end) as a_f,
       max(case when col1 = 'a' then g end) as a_g,
       max(case when col1 = 'b' then e end) as b_e,
       max(case when col1 = 'b' then f end) as b_f,
       max(case when col1 = 'b' then g end) as b_g,
       max(case when col1 = 'c' then e end) as c_e,
       max(case when col1 = 'c' then f end) as c_f,
       max(case when col1 = 'c' then g end) as c_g,
       max(case when col1 = 'd' then e end) as d_e,
       max(case when col1 = 'd' then f end) as d_f,
       max(case when col1 = 'd' then g end) as d_g
from t;

谢谢你的回答。我以前试过这个,但它没有给出我想要的确切结果。将不匹配的记录返回为NULL。我已将输出添加到问题中。如您所见,使用Case-when时返回空值。@uckocaman。除非使用聚合函数,否则条件聚合不是聚合。我修正了答案。谢谢你的关注和回复。