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

Sql 将数据拆分为两列的步骤

Sql 将数据拆分为两列的步骤,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我将数据按升序排列在一列中。我想把它分成两列。检查我的表和预期输出。这背后的逻辑是,我希望在我的第一列和第二、第四和第六列中有第1、第3、第5行数据,依此类推。。在我的第二栏中的数据 我的现有表: 表1: Col1 2012 2013 2014 2015 2016 2017 Col1 Col2 2012 2013 2014 2015 2016 2017 预期输出: Col1 2012 2013 2014 2015 2016 2017 Col1 Col2 201

我将数据按升序排列在一列中。我想把它分成两列。检查我的表和预期输出。这背后的逻辑是,我希望在我的第一列和第二、第四和第六列中有第1、第3、第5行数据,依此类推。。在我的第二栏中的数据

我的现有表:
表1:

Col1
2012
2013
2014
2015
2016
2017
Col1    Col2
2012    2013
2014    2015
2016    2017
预期输出:

Col1
2012
2013
2014
2015
2016
2017
Col1    Col2
2012    2013
2014    2015
2016    2017

任何人对我如何实现这一目标有任何建议。

我想你可以使用这样的查询:

SELECT 
    Max(Col1) Col1, Max(Col2) Col2
FROM (
    SELECT 
        (ROW_NUMBER() OVER (ORDER BY Col1)- 1) / 2 as seq,
        CASE WHEN ROW_NUMBER() OVER (ORDER BY Col1) % 2 = 1 THEN Col1 END Col1, 
        CASE WHEN ROW_NUMBER() OVER (ORDER BY Col1) % 2 = 0 THEN Col1 END Col2
    FROM yourTable) t
GROUP BY seq