简体   繁体   中英

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. I start out with a JSON string stored in a single column, looking like this...

查询一

I then can use this query to extract just the Ids portion of each record...

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.

I need to use that list of Ids for passing to a stored procedure as well as getting a count of the Ids.

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, '');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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