根据列拆分行并粘贴到不同的表
Split Row and Paste to Different Tables Based on Column
我有一个 table 这样的。每次订单完成时都会填充 Table。一份订单可以有一个或多个隔间。
+---------+-------+-------------+------+
| OrderID | Plant | Compartment | Qty |
+---------+-------+-------------+------+
| 91 | 12 | 1 | 2000 |
| 91 | 12 | 2 | 2000 |
| 91 | 12 | 3 | 2000 |
| 90 | 12 | 1 | 3000 |
| 89 | 12 | 1 | 5000 |
+---------+-------+-------------+------+
请帮助编写一个 SQL 脚本,将上面的内容拆分成两个新的 table,如下所示:
Table 1
+---------+-------+
| OrderID | Plant |
+---------+-------+
| 91 | 12 |
| 90 | 12 |
| 89 | 12 |
+---------+-------+
Table 2
+---------+-------------+------+
| OrderID | Compartment | Qty |
+---------+-------------+------+
| 91 | 1 | 2000 |
| 91 | 2 | 2000 |
| 91 | 3 | 2000 |
| 90 | 1 | 3000 |
| 89 | 1 | 5000 |
+---------+-------------+------+
我试过按照建议使用 DISTINCT 命令;
SELECT * FROM table
WHERE [OrderID] = (SELECT DISTINCT OrderID from table where (COMPARTMENT = '1'))
其中returns错误;
子查询返回了 1 个以上的值。当子查询跟在 =、!=、<、<=、>、>= 或子查询用作表达式时,这是不允许的。
如果脚本能够跟踪已经处理过的行,从而避免每次运行时出现重复,那就锦上添花了。
我会这样做:
-- just for debugging, your table is my @table variable table
set nocount on
declare @table table (OrderId int, Plant int, Compartment int, Qty int)
insert into @table values (89, 12, 1, 5000)
insert into @table values (90, 12, 1, 3000)
insert into @table values (91, 12, 3, 2000)
insert into @table values (91, 12, 2, 2000)
insert into @table values (91, 12, 1, 2000)
insert into @table values (91, 12, 2, 3000)
set nocount off
--select * from @table
-- now here it comes selections:
if (exists (select *
from INFORMATION_SCHEMA.TABLES
where TABLE_SCHEMA = 'dbo' -- or your schema
and TABLE_NAME = 'THeader' -- or your header's table))
insert into THeader
select distinct t1.OrderId, t1.Plant
from @table t1
left join THeader t2 on t1.OrderId = t2.OrderId and t1.Plant = t2.Plant
where t2.OrderId is null
else
select distinct t1.OrderId, t1.Plant
into THeader
from @table t1
left join THeader t2 on t1.OrderId = t2.OrderId and t1.Plant = t2.Plant
where t2.OrderId is null
if (exists (select *
from INFORMATION_SCHEMA.TABLES
where TABLE_SCHEMA = 'dbo' -- or your schema
and TABLE_NAME = 'TChilds' -- or your childs's table))
insert into TChilds
select distinct t1.OrderId, t1.Compartment, t1.Qty
from @table t1
left join TChilds t2 on t1.OrderId = t2.OrderId and t1.Compartment = t2.Compartment and t1.Qty = t2.Qty
where t2.OrderId is null
else
select distinct t1.OrderId, t1.Compartment, t1.Qty
into TChilds
from @table t1
left join TChilds t2 on t1.OrderId = t2.OrderId and t1.Compartment = t2.Compartment and t1.Qty = t2.Qty
where t2.OrderId is null
-- just for debugging
select * from THeader
select * from TChilds
编辑: 即便如此,我还是把你的 master table 看作是一个 child table,你必须从那里创建 Header 的 table。够了。我的意思是你的 Table 可以是我的 TChilds table,key 可以是 OrderId + Plant。 (我不知道植物在这table是什么意思)
我有一个 table 这样的。每次订单完成时都会填充 Table。一份订单可以有一个或多个隔间。
+---------+-------+-------------+------+
| OrderID | Plant | Compartment | Qty |
+---------+-------+-------------+------+
| 91 | 12 | 1 | 2000 |
| 91 | 12 | 2 | 2000 |
| 91 | 12 | 3 | 2000 |
| 90 | 12 | 1 | 3000 |
| 89 | 12 | 1 | 5000 |
+---------+-------+-------------+------+
请帮助编写一个 SQL 脚本,将上面的内容拆分成两个新的 table,如下所示:
Table 1
+---------+-------+
| OrderID | Plant |
+---------+-------+
| 91 | 12 |
| 90 | 12 |
| 89 | 12 |
+---------+-------+
Table 2
+---------+-------------+------+
| OrderID | Compartment | Qty |
+---------+-------------+------+
| 91 | 1 | 2000 |
| 91 | 2 | 2000 |
| 91 | 3 | 2000 |
| 90 | 1 | 3000 |
| 89 | 1 | 5000 |
+---------+-------------+------+
我试过按照建议使用 DISTINCT 命令;
SELECT * FROM table
WHERE [OrderID] = (SELECT DISTINCT OrderID from table where (COMPARTMENT = '1'))
其中returns错误;
子查询返回了 1 个以上的值。当子查询跟在 =、!=、<、<=、>、>= 或子查询用作表达式时,这是不允许的。
如果脚本能够跟踪已经处理过的行,从而避免每次运行时出现重复,那就锦上添花了。
我会这样做:
-- just for debugging, your table is my @table variable table
set nocount on
declare @table table (OrderId int, Plant int, Compartment int, Qty int)
insert into @table values (89, 12, 1, 5000)
insert into @table values (90, 12, 1, 3000)
insert into @table values (91, 12, 3, 2000)
insert into @table values (91, 12, 2, 2000)
insert into @table values (91, 12, 1, 2000)
insert into @table values (91, 12, 2, 3000)
set nocount off
--select * from @table
-- now here it comes selections:
if (exists (select *
from INFORMATION_SCHEMA.TABLES
where TABLE_SCHEMA = 'dbo' -- or your schema
and TABLE_NAME = 'THeader' -- or your header's table))
insert into THeader
select distinct t1.OrderId, t1.Plant
from @table t1
left join THeader t2 on t1.OrderId = t2.OrderId and t1.Plant = t2.Plant
where t2.OrderId is null
else
select distinct t1.OrderId, t1.Plant
into THeader
from @table t1
left join THeader t2 on t1.OrderId = t2.OrderId and t1.Plant = t2.Plant
where t2.OrderId is null
if (exists (select *
from INFORMATION_SCHEMA.TABLES
where TABLE_SCHEMA = 'dbo' -- or your schema
and TABLE_NAME = 'TChilds' -- or your childs's table))
insert into TChilds
select distinct t1.OrderId, t1.Compartment, t1.Qty
from @table t1
left join TChilds t2 on t1.OrderId = t2.OrderId and t1.Compartment = t2.Compartment and t1.Qty = t2.Qty
where t2.OrderId is null
else
select distinct t1.OrderId, t1.Compartment, t1.Qty
into TChilds
from @table t1
left join TChilds t2 on t1.OrderId = t2.OrderId and t1.Compartment = t2.Compartment and t1.Qty = t2.Qty
where t2.OrderId is null
-- just for debugging
select * from THeader
select * from TChilds
编辑: 即便如此,我还是把你的 master table 看作是一个 child table,你必须从那里创建 Header 的 table。够了。我的意思是你的 Table 可以是我的 TChilds table,key 可以是 OrderId + Plant。 (我不知道植物在这table是什么意思)