TSQL 我如何检查数组是使用 JSON_QUERY 的子集
TSQL How do i check array is a subset using JSON_QUERY
我有以下 Table Documents
,其中数据以 JSON 格式存储。
DocumentID Status Data
------------------------------------------
1 Active '{ "AccountNumber":["A1","A2","A3","A4"] }'
2 Active '{ "AccountNumber":["A1","A3"] }'
3 Active '{ "AccountNumber":["A2","A4"] }'
4 Active '{ "AccountNumber":["A1"] }'
然后我有过滤器,它也是 json
DECLARE @filter = '{ "AccountNumber":["A2","A3"] }'
如何在 where 子句中应用过滤器。
预期结果应该 return 匹配文档,DocumentID 1,2,3
SELECT DocumentID
FROM Documents D
WHERE
JSON_QUERY(D.Data,'$.AccountNumber') IN JSON_QUERY($(@filter,'$.AccountNumber') -- This is not working
select DocumentID
from
Documents D
where
exists (select value from openjson(D.data, '$.AccountNumber')
intersect
select value from openjson(@filter, '$.AccountNumber')
)
;
select distinct DocumentID
from
Documents D
cross apply openjson(D.data, '$.AccountNumber') j
inner join openjson(@filter, '$.AccountNumber') f on j.value = f.value
;
我有以下 Table Documents
,其中数据以 JSON 格式存储。
DocumentID Status Data
------------------------------------------
1 Active '{ "AccountNumber":["A1","A2","A3","A4"] }'
2 Active '{ "AccountNumber":["A1","A3"] }'
3 Active '{ "AccountNumber":["A2","A4"] }'
4 Active '{ "AccountNumber":["A1"] }'
然后我有过滤器,它也是 json
DECLARE @filter = '{ "AccountNumber":["A2","A3"] }'
如何在 where 子句中应用过滤器。
预期结果应该 return 匹配文档,DocumentID 1,2,3
SELECT DocumentID
FROM Documents D
WHERE
JSON_QUERY(D.Data,'$.AccountNumber') IN JSON_QUERY($(@filter,'$.AccountNumber') -- This is not working
select DocumentID
from
Documents D
where
exists (select value from openjson(D.data, '$.AccountNumber')
intersect
select value from openjson(@filter, '$.AccountNumber')
)
;
select distinct DocumentID
from
Documents D
cross apply openjson(D.data, '$.AccountNumber') j
inner join openjson(@filter, '$.AccountNumber') f on j.value = f.value
;