我有这张桌子:
CREATE TABLE [BGIA].[INTCOL004VA](
[ID] [int] NOT NULL,
[DATYEN] [varchar](18) NULL,
[ZZRIDN] [varchar](8) NULL,
[OQTIEF] [varchar](1) NULL,
[OQMONE] [varchar](3) NULL,
[ZZPONR] [varchar](3) NULL,
[ZZNBRE] [varchar](15) NULL,
[ZZPDSE] [varchar](22) NULL
);
INSERT INTO INTCOL004VA VALUES
('53671955', 'MC1141103006', '38565363', '1', '007', '010', '0', '0'),
('53671956', 'MC1141103006', '38565363', '0', NULL, '020', '0', '0'),
('53671957', 'MC1141103006', '38565363', '0', '007', '030', '1', '500'),
('53671958', 'VT1141103010', '38565421', '1', '007', '050', '0', '0'),
('53671959', 'VT1141103010', '38565421', '0', NULL, '100', '0', '0'),
('53671960', 'ST1141103006', '38587542', '0', NULL, '010', '1', '500'),
('53671961', 'ST1141103006', '38587542', '1', 'B01', '020', '5', '0');
记录由DATYEN,ZZRIDN和ZZPONR链接
我希望更新表符合此规则:
我无法使用INNER JOIN,突然间我不知道如何成功
谢谢:)
编辑:例如:
('53671955', 'MC1141103006', '38565363', '1', '007', '010', '0', '0'),
('53671956', 'MC1141103006', '38565363', '0', NULL, '020', '0', '0'),
('53671957', 'MC1141103006', '38565363', '0', '007', '030', '1', '500')
必须:
('53671955', 'MC1141103006', '38565363', '0', NULL, '010', '0', '0'),
('53671956', 'MC1141103006', '38565363', '0', NULL, '020', '0', '0'),
('53671957', 'MC1141103006', '38565363', '0', NULL, '030', '1', '500')
因为组MC1141103006和38565363有一行ZZNBRE而ZZPDSE不是0
这:
('53671960', 'ST1141103006', '38587542', '0', NULL, '010', '1', '500'),
('53671961', 'ST1141103006', '38587542', '1', 'B01', '020', '5', '0')
必须:
('53671960', 'ST1141103006', '38587542', '0', NULL, '010', '1', '500'),
('53671961', 'ST1141103006', '38587542', '0', NULL, '020', '0', '0')
因为组ST1141103006和38587542有一行ZZNBRE且ZZPDSE不为0但将ZZNBRE设置为0,因为ZZPDSE为0
而且:
('53671958', 'VT1141103010', '38565421', '1', '007', '050', '0', '0'),
('53671959', 'VT1141103010', '38565421', '0', NULL, '100', '0', '0')
必须:
('53671958', 'VT1141103010', '38565421', '1', '007', '050', '0', '0'),
('53671959', 'VT1141103010', '38565421', '1', '007', '100', '0', '0')
因为所有行都有ZZNBRE且ZZNBRE为0
我希望确切地说,我想这很难理解:(
编辑2:其实我的查询是:
update v1
set v1.zznbre = (case when v1.zzpdse = 0 then 0 else v1.zznbre end),
v1.oqtief = (case when v1.zznbre = 0 and v2.zznbre = 0 and v1.zzpdse = 0 and v2.zzpdse = 0 then 1 else 0 end),
v1.oqmone = (case when v1.zznbre = 0 and v2.zznbre = 0 and v1.zzpdse = 0 and v2.zzpdse = 0 then v2.oqmone else null end)
from intcol004va v1
inner join intcol004va v2 on v2.datyen = v1.datyen and v2.zzridn = v1.zzridn and v2.id <> v1.id
where v2.oqtief = 1 and v2.oqmone is not null and v1.oqtief <> v2.oqtief
但在所有情况下都无法正常工作:(
答案 0 :(得分:0)
如果您将更新作为两个独立更新处理,则会生成正确的答案集。 我提供了一个结果表和代码,用于显示原始数据与您描述中定义的所需结果表之间的比较。
接下来,它将执行两个更新,然后显示已连接的原始表和目标表,以证明它们已正确对齐。
CREATE TABLE [INTCOL004VA](
[ID] [int] NOT NULL,
[DATYEN] [varchar](18) NULL,
[ZZRIDN] [varchar](8) NULL,
[OQTIEF] [varchar](1) NULL,
[OQMONE] [varchar](3) NULL,
[ZZPONR] [varchar](3) NULL,
[ZZNBRE] [varchar](15) NULL,
[ZZPDSE] [varchar](22) NULL
);
CREATE TABLE [R_INTCOL004VA](
[ID] [int] NOT NULL,
[DATYEN] [varchar](18) NULL,
[ZZRIDN] [varchar](8) NULL,
[OQTIEF] [varchar](1) NULL,
[OQMONE] [varchar](3) NULL,
[ZZPONR] [varchar](3) NULL,
[ZZNBRE] [varchar](15) NULL,
[ZZPDSE] [varchar](22) NULL
);
INSERT INTO INTCOL004VA VALUES
('53671955', 'MC1141103006', '38565363', '1', '007', '010', '0', '0'),
('53671956', 'MC1141103006', '38565363', '0', NULL, '020', '0', '0'),
('53671957', 'MC1141103006', '38565363', '0', '007', '030', '1', '500'),
('53671958', 'VT1141103010', '38565421', '1', '007', '050', '0', '0'),
('53671959', 'VT1141103010', '38565421', '0', NULL, '100', '0', '0'),
('53671960', 'ST1141103006', '38587542', '0', NULL, '010', '1', '500'),
('53671961', 'ST1141103006', '38587542', '1', 'B01', '020', '5', '0');
INSERT INTO [R_INTCOL004VA] VALUES
('53671955', 'MC1141103006', '38565363', '0', NULL, '010', '0', '0'),
('53671956', 'MC1141103006', '38565363', '0', NULL, '020', '0', '0'),
('53671957', 'MC1141103006', '38565363', '0', NULL, '030', '1', '500'),
('53671960', 'ST1141103006', '38587542', '0', NULL, '010', '1', '500'),
('53671961', 'ST1141103006', '38587542', '0', NULL, '020', '0', '0'),
('53671958', 'VT1141103010', '38565421', '1', '007', '050', '0', '0'),
('53671959', 'VT1141103010', '38565421', '1', '007', '100', '0', '0')
SELECT
D.ID,
D.DATYEN,
D.ZZRIDN,
D.OQTIEF,
E.OQTIEF AS CORRECT_OQTIEF,
D.OQMONE,
E.OQMONE AS CORRECT_OQMONE,
D.ZZPONR,
D.ZZNBRE,
D.ZZPDSE
FROM
INTCOL004VA AS D
INNER JOIN
R_INTCOL004VA AS E ON D.ID = E.ID
UPDATE
INTCOL004VA
SET
OQTIEF = 0,
OQMONE = NULL
FROM
INTCOL004VA AS A
INNER JOIN
(SELECT
B.DATYEN,
B.ZZRIDN
FROM
INTCOL004VA AS B
GROUP BY
B.DATYEN,
B.ZZRIDN
HAVING
SUM(CAST(B.ZZNBRE AS INT)) > 0
) AS C ON A.DATYEN = C.DATYEN AND A.ZZRIDN = C.ZZRIDN
UPDATE
INTCOL004VA
SET
OQTIEF = 1,
OQMONE = C.OQMONE
FROM
INTCOL004VA AS A
INNER JOIN
(SELECT
B.DATYEN,
B.ZZRIDN,
MAX(ISNULL(B.OQMONE,'')) AS OQMONE
FROM
INTCOL004VA AS B
GROUP BY
B.DATYEN,
B.ZZRIDN
HAVING
SUM(CAST(B.OQTIEF AS INT)) > 0
) AS C ON A.DATYEN = C.DATYEN AND A.ZZRIDN = C.ZZRIDN
SELECT
D.ID,
D.DATYEN,
D.ZZRIDN,
D.OQTIEF,
E.OQTIEF AS CORRECT_OQTIEF,
D.OQMONE,
E.OQMONE AS CORRECT_OQMONE,
D.ZZPONR,
D.ZZNBRE,
D.ZZPDSE
FROM
INTCOL004VA AS D
INNER JOIN
R_INTCOL004VA AS E ON D.ID = E.ID
有时将其分解成更小的部分是解决问题的更简单方法。