【发布时间】:2013-06-19 19:13:54
【问题描述】:
我需要在更新表期间应用一些逻辑。
existing_items 是目标表,received_items 保存对 existing_items 或新项目的更新。
逻辑是 - 对于每个分组的 received_items,应该在 existing_items 中标识一个匹配的行。如果未找到匹配项,则应创建一个新行。
关键在于行可以匹配多个条件。它们应该始终在代码、line_no(如果给定)、ref(如果给定)上匹配。 received_items 行应按 processing_seq 顺序处理,并可能按给定顺序检查匹配项。
当所有分组的 received_item 都与一个 existing_item 匹配时,任何剩余的都是新行。
给定:
create table #existing_items(id int identity(1,1), code varchar(10)
,qty numeric(10,2), line_no int, ref varchar(10))
create table #received_items(code varchar(10), qty numeric(10,2), line_no int
,ref varchar(10), processing_seq int)
insert into #existing_items (code, qty, line_no, ref)
values('ABC123',2.0, 1, NULL)
insert into #existing_items (code, qty, line_no, ref)
values('ABC123',3.0, 2, '1001')
insert into #received_items(code, qty, line_no, ref, processing_seq)
values ('ABC123', 4, NULL, NULL, 1)
insert into #received_items(code, qty, line_no, ref, processing_seq)
values ('ABC123', 3, NULL, NULL, 1)
insert into #received_items(code, qty, line_no, ref, processing_seq)
values ('ABC123', 4, NULL, 1002, 2)
insert into #received_items(code, qty, line_no, ref, processing_seq)
values ('ABC123', 4, 2, 1003, 3)
insert into #received_items(code, qty, line_no, ref, processing_seq)
values ('ABC123', 5, NULL, NULL, 4)
从 #received_items 中选择 *
ABC123 4.00 NULL NULL 1
ABC123 3.00 NULL NULL 1
ABC123 4.00 NULL 1003 2
ABC123 4.00 2 1002 3
ABC123 5.00 NULL NULL 4
从 #existing_items 中选择 *
1 ABC123 2.00 1 NULL
2 ABC123 3.00 2 1001
结果应该是:
1 ABC123 7.00 1 NULL
2 ABC123 4.00 2 1002
3 ABC123 4.00 3 1003
4 ABC123 5.00 4 NULL
解释一下:
id=1 的existing_items 更新为7,因为 received_items 应该被分组(code、line_no、ref、processing_seq)。该行仅在代码上匹配,因为没有提供 line_no 或 ref。
创建了一个 id=3 的新项目,因为没有找到与 ref 1003 匹配的项目。
id=2 的existing_items 更新qty 和ref,因为在line_no 上找到了匹配项。
创建了一个 id=4 的新行,因为没有要匹配的行(id=1 已经与 processing_seq = 1 的第一组匹配)。
不知道该怎么做,正在考虑使用光标,但可能有更简单的方法。我目前正在处理多个自我连接.. 像这样:
Select grp.*
,fm.id as full_match, rm.id as ref_match, lm.id as line_match
,(select min(id) from #existing_items where code = grp.code
and rm.id IS NULL and lm.id IS NULL and fm.id IS NULL and grp.ref IS NULL
) as code_match
-- ,cm.id as code_match
FROM (
select ri.code, sum(ri.qty) qty,ri.line_no,ri.ref, ri.processing_seq
from #received_items ri
group by code, line_no, ref, processing_seq
) grp
LEFT OUTER JOIN
#existing_items fm
ON grp.code = fm.code AND grp.line_no = fm.line_no and grp.ref = fm.ref
LEFT OUTER JOIN
#existing_items rm
ON grp.code = rm.code AND grp.ref = rm.ref
LEFT OUTER JOIN
#existing_items lm
ON grp.code = lm.code AND grp.line_no = lm.line_no
order by grp.processing_seq
这可以部分了解要更新的行并产生这个中间结果:
Code Qty Line_No Ref seq fm.id rm.id lm.id cm.id
ABC123 7.00 NULL NULL 1 NULL NULL NULL 1
ABC123 4.00 NULL 1002 2 NULL NULL NULL NULL
ABC123 4.00 2 1003 3 NULL NULL 2 NULL
ABC123 5.00 NULL NULL 4 NULL NULL NULL 1
需要一种方法来识别仅在代码上最接近的匹配,这已经奏效,但不适用于 seq=4,它的 cm.id 应该为 NULL - 所以我需要更改我的子查询以不返回之前匹配的 id相同的子查询?然后我应该能够在任何匹配列中没有 id 的地方插入。
非常感谢您对如何解决问题的任何见解。
【问题讨论】:
-
.... 为什么“数量”是
numeric字段?你能在逻辑上拥有这些东西的“一半”吗?在几乎所有情况下,数量都应该是整数(没有分数)。当您确实有部分金额时,通常是其他内容,并且需要指定单位。除此之外,这是间隙和孤岛问题的变体;你有哪个版本的 SQL Server,有些版本对此有更好的支持。 -
我为了解释问题而编造了它。数量通常可以以平方米、立方等为单位,我在这里没有说明。 sql 2005
-
如果我看到标记为
quantity的列,我将假设一个整数计数,句点。如果还有其他问题,我将寻找所描述的测量值(并希望使用单位,无论是在类型信息中还是在列名中)——即volume、volume_in_meters_cubed等。如果你是尝试将两者存储在同一列中,以后会很痛苦.... -
是的。我没有存储在同一列中。此外,这与问题无关,如果您能提供帮助,将不胜感激。称它为小部件或任何有帮助的东西。
标签: sql sql-server sql-server-2005 gaps-and-islands