SQL 服务器插入行:每 "group" 插入一行
SQL Server Insert Row: Inserting a row per "group"
我想根据 "Material" 列将多行插入到 table 中。下面附上 table:
的片段
+----------+---------+-----------+
| Material | Station | BuildTime |
+----------+---------+-----------+
| ABC | #1 | 5 |
| ABC | #2 | 10 |
| ABC | #3 | 15 |
| DEF | #1 | 7 |
| DEF | #2 | 12 |
| DEF | #3 | 19 |
| GHI | #1 | 11 |
| GHI | #2 | 24 |
| GHI | #3 | 13 |
+----------+---------+-----------+
如您所见,在三个不同的车站有三种类型的 material - 总共有 9 个条目。假设我想为每种 material 添加一个 "Station #4" 行。如何插入每个 material "group"?
在我的特定情况下,第 4 站的 "buildtime" 值将全部与值 50 相同。
如果真的只有三个 material 组,这看起来很简单,但实际上有数百个。有没有办法解析它们并为每个组插入一行?
提前致谢。
您可以使用 insert
:
insert into t (material, station, buildtime)
select distinct material, '#4', 50
from t;
我想根据 "Material" 列将多行插入到 table 中。下面附上 table:
的片段+----------+---------+-----------+
| Material | Station | BuildTime |
+----------+---------+-----------+
| ABC | #1 | 5 |
| ABC | #2 | 10 |
| ABC | #3 | 15 |
| DEF | #1 | 7 |
| DEF | #2 | 12 |
| DEF | #3 | 19 |
| GHI | #1 | 11 |
| GHI | #2 | 24 |
| GHI | #3 | 13 |
+----------+---------+-----------+
如您所见,在三个不同的车站有三种类型的 material - 总共有 9 个条目。假设我想为每种 material 添加一个 "Station #4" 行。如何插入每个 material "group"?
在我的特定情况下,第 4 站的 "buildtime" 值将全部与值 50 相同。
如果真的只有三个 material 组,这看起来很简单,但实际上有数百个。有没有办法解析它们并为每个组插入一行?
提前致谢。
您可以使用 insert
:
insert into t (material, station, buildtime)
select distinct material, '#4', 50
from t;