Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.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 - Fatal编程技术网

在SQL Server中,将多行放入列中,列值按列分组

在SQL Server中,将多行放入列中,列值按列分组,sql,sql-server,Sql,Sql Server,如何通过以下方式将行转换为列组: 预期结果如下所示: 如果有一列指定排序,则可以使用条件聚合: select max(case when columnname = 'SupplierGSTin' then value end) as SupplierGSTin, max(case when columnname = 'DocumentNumber' then value end) as DocumentNumber, max(case when columnname

如何通过以下方式将行转换为列组:

预期结果如下所示:


如果有一列指定排序,则可以使用条件聚合:

select max(case when columnname = 'SupplierGSTin' then value end) as SupplierGSTin,
       max(case when columnname = 'DocumentNumber' then value end) as DocumentNumber,
       max(case when columnname = 'SupplyType' then value end) as SupplyType
from (select t.*,
             row_number() over (partition by columnname order by ?) as seqnum
      from t
     ) t
group by rownum;

用于指定排序的列。

它是动态的,因此我们不能假设列名值固定在post picture之外,将数据作为DDL和DML语句发布,并且不要忘记包含您的尝试。关于如何做到这一点,有很多例子,那么这些答案有什么错呢?@ShankarKumar。SQL表表示无序集。您似乎假设您的数据具有顺序。除非列具有顺序,否则转换没有意义。