簡體   English   中英

獲取 MS SQL 服務器中多個 JSON 字符串中數組中多個 Int 的計數

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

我一直在搜索這個主題,我找到了處理字符串的例子,但關於 arrays 的例子不多。 我從存儲在單個列中的 JSON 字符串開始,看起來像這樣......

查詢一

然后我可以使用這個查詢來提取每條記錄的 Ids 部分......

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

識別結果

我現在需要將那些 arrays(不確定它們是否是看起來像 arrays 的字符串?)組合成一個數組。

我需要使用該 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