【问题标题】:Split Row and Paste to Different Tables Based on Column根据列拆分行并粘贴到不同的表
【发布时间】:2020-06-11 04:54:17
【问题描述】:

我有一张这样的桌子。每次订单完成时都会填充表格。一份订单可以有一个或多个隔间。

+---------+-------+-------------+------+
| 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 脚本,将上述内容拆分为两个新表,如下所示:

表 1

+---------+-------+
| OrderID | Plant |
+---------+-------+
|      91 |    12 |
|      90 |    12 |
|      89 |    12 |
+---------+-------+

表 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'))

返回错误;

子查询返回超过 1 个值。当子查询跟随 =、!=、、>= 或子查询用作表达式时,这是不允许的。

如果脚本可以跟踪已处理的行以避免每次运行时重复,那将是锦上添花。

【问题讨论】:

  • 请格式化您的数据。谢谢!
  • 到目前为止你尝试过什么?为什么它不起作用? SO 不是免费的编码服务,因此请至少向我们展示您对该主题的研究。这看起来就像你在 2 个 SELECT 语句之后,一个带有 DISTINCT 的语句。
  • 到目前为止我尝试过的都没有工作的机会。抱歉,我对 SQL 的经验很少,我从事网络安全工作,偶尔会处理 SAP MII。

标签: sql sql-server split rows


【解决方案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

编辑: 即便如此,我仍将您的主表视为子表,您必须从中创建 Header 表。够了。我的意思是你的表可以是我的 TChilds 表,键可以是 OrderId + Plant。 (我不知道这张表里的植物是什么意思)

【讨论】:

  • 非常感谢您抽出宝贵时间回复。我要给自己煮一杯咖啡,然后我要坐下来消化这个。我会发回我的发现。
  • 您好 Vali,只是想说您的解决方案非常有效!非常感谢你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-21
  • 2012-06-11
  • 2018-08-30
  • 1970-01-01
  • 2017-09-21
  • 2020-04-22
相关资源
最近更新 更多