将1行拆分为多行

时间:2014-03-30 13:18:10

标签: sql database tsql database-design relational-database

数据

1; from 0; to 1; 5% ; boulder; 10% ; sand; 20%; silt

输出应如下所示:

1; from 0; to 1;  5% ; boulder

1; from 0; to 1; 10%; sand

1; from 0; to 1; 20% silt

1 个答案:

答案 0 :(得分:0)

您可以使用unpivot执行此操作。但是,如果数据不是太大,最简单的方法就是使用union all

select station, "from(m)", "to(m)", "ob.1" as Materials, Percent1 as Percent
from table t
union all
select station, "from(m)", "to(m)", "ob.2" as Materials, Percent2 as Percent
from table t
union all
select station, "from(m)", "to(m)", "ob.3" as Materials, Percent3 as Percent
from table t;

请注意,您有三列名为Percent。这在SQL表中是不允许的,所以我假设它们有不同的名称。

如果您的表格非常大,使用cross joinunpivot的另一种方法通常会有更好的效果。

相关问题