简体   繁体   English

获取 MS SQL 服务器中多个 JSON 字符串中数组中多个 Int 的计数

[英]Get count of multiple Ints in array in multiple JSON strings in MS SQL Server

I have been searching this topic and I am finding examples that deal with strings but not so much about arrays.我一直在搜索这个主题,我找到了处理字符串的例子,但关于 arrays 的例子不多。 I start out with a JSON string stored in a single column, looking like this...我从存储在单个列中的 JSON 字符串开始,看起来像这样......

查询一

I then can use this query to extract just the Ids portion of each record...然后我可以使用这个查询来提取每条记录的 Ids 部分......

SELECT Id, Message,
    JSON_QUERY(Message, 'strict$.Ids') AS Ids
FROM ActionQueue
WHERE Status = 1
    AND (Action = 'Approve' OR Action = 'Reject')

识别结果

I need to now combine those arrays (not sure if they are strings that look like arrays?) into a single array.我现在需要将那些 arrays(不确定它们是否是看起来像 arrays 的字符串?)组合成一个数组。

I need to use that list of Ids for passing to a stored procedure as well as getting a count of the Ids.我需要使用该 Id 列表来传递给存储过程以及获取 Id 的计数。

declare @t table(thejsoncolumn nvarchar(max));

insert into @t(thejsoncolumn)
values(N'{"Ids":[1, 2, 3, 4, 5]}'), (N'{"Ids":[7]}'), (N'{"Ids":[6, 9, 10, 11]}');

select stuff(
(select concat(',', value)
--string_agg(value, ',') within group (order by cast(value as int)) AS thelist
from
(
select distinct j.value
from @t as t
cross apply openjson(t.thejsoncolumn, '$.Ids') as j
) as src
order by cast(value as int)
for xml path('') --..numbers only
), 1, 1, '');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM