如果下面提到的任何目标之间的数据为空,我想将doprocess标志设置为0
示例:
create table TestSAMP (
id int identity(1,1),
modelid navrchar(max),
target1 nvarchar(max),
target2 nvarchar(max),
target3 nvarchar(max),
target4 nvarchar(max),
doprcoess int default(1)
)
--VALID SET DOPROCESS FLAG TO 1
INSERT INTO TestSAMP(modelid,target1,target2,target3,target4)
VALUES('1','T1','T2','T3','T4')
--NOTVALID SET DOPROCESS FLAG TO 0 DUE TO THE DATA IS MISSING IN SEQUENCE
INSERT INTO TestSAMP(modelid,target1,target2,target3,target4)
VALUES('2','TR','','T3','T4')
--VALID SET DOPROCESS FLAG TO 1 As if data is present
-- it should be present insequence in below t1 t2 as they arfe in sequence
INSERT INTO TestSAMP(modelid,target1,target2,target3,target4)
VALUES('3','T1','T2','','')
--NOTVALID SET DOPROCESS FLAG TO 0 DUE TO THE DATA IS MISSING IN SEQUENCE
--where T4 data is provided and not in T3
INSERT INTO TestSAMP(modelid,target1,target2,target3,target4)
VALUES('4','T1','T2','','T4')
我有很多解决方案,人们可以尝试找到顺序 数字,但这里是nvarchar
答案 0 :(得分:1)
您可以将间隙和岛视为字符串,即“ IGGI”,将其压扁,然后搜索“ IGI”模式:
SELECT *, CASE WHEN squashed LIKE '%IGI%' THEN 0 ELSE 1 END AS new_doprocess
FROM TestSAMP t
CROSS APPLY(SELECT STRING_AGG(CASE WHEN t = '' THEN 'G' ELSE 'I'END, '') r
FROM (VALUES (1,id, target1), (2,id, target2),
(3,id, target3), (4,id, target4)) sub(rn, id, t)) s
CROSS APPLY (SELECT replace(replace(replace(replace(replace(replace(s.r,'G','<>')
,'><',''),'<>','G'),'I','<>'),'><',''),'<>','I')) AS sub(squashed)
ORDER BY t.id;
示例:
id | target1 | target2 | target3 | target4 | r | squashed | doprocess
-----|----------|----------|----------|----------|-------|-----------|-----------
1 | T1 | T2 | T3 | T4 | IIII | I | 1
2 | T1 | | T3 | T4 | IGII | IGI | 0
3 | T1 | T2 | | | IIGG | IG | 1
4 | T1 | T2 | | T4 | IIGI | IGI | 0
5 | | | | T4 | GGGI | GI | 1
6 | | | | | GGGG | G | 1
答案 1 :(得分:0)
最简单的方法应该是布尔表达式,以检查后继者是否为空时前任是否为空。
UPDATE testsamp
SET doprocess = 0
WHERE target2 <> ''
AND target1 = ''
OR target3 <> ''
AND target2 = ''
OR target4 <> ''
AND target3 = '';
例如,当所有目标均为空时,如果它也无效,则可能要扩展它。我不知道你是否想要那个。