SQL Server中的行-列操作
我有一张这样的桌子: 我想从下表中得到一个结果。如何在SQL Server中执行此操作 使用Case时的输出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
您可以使用条件聚合:
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。除非使用聚合函数,否则条件聚合不是聚合。我修正了答案。谢谢你的关注和回复。