Вы можете распаковать JSON с помощью метода OPENJSON
, а затем собрать его с помощью STRING_AGG
и JSON_QUERY
. Простой пример:
USE tempdb
GO
DROP TABLE IF EXISTS jsonWorking;
CREATE TABLE jsonWorking (
Id INT PRIMARY KEY,
UserId INT NOT NULL,
Platforms NVARCHAR(MAX) NOT NULL,
TransactionTypeId NVARCHAR(MAX) NOT NULL
)
GO
INSERT INTO jsonWorking ( Id, UserId, Platforms, TransactionTypeId )
VALUES
( 0, 1, '{"p3":1,"p1":1}', '{"t1":1,"t2":1}' ),
( 1, 2, '{"p3":2}', '{"t2":1,"t1":1}' ),
( 3, 1, '{"p2":1}', '{"t1":1}' )
GO
-- Crack open the json WITH OPENJSON, reassemble it with STRING_AGG and JSON_QUERY
;WITH platforms AS
(
SELECT Id, UserId, [key], [value]
FROM jsonWorking
CROSS APPLY OPENJSON ( Platforms )
)
SELECT MIN(Id) Id, UserId,
JSON_QUERY( '{' + STRING_AGG( '"' + [key] + '"' + ':' + [value], ',' ) + '}' ) yourJson
FROM platforms
GROUP BY UserId
Мои результаты:
Расширить технику на два столбца JSON немного сложнее, но возможно, например,
;WITH p1 AS
(
SELECT MIN(Id) Id, UserId, [key], SUM( CAST( [value] AS INT ) ) [value]
FROM jsonWorking
CROSS APPLY OPENJSON ( Platforms )
GROUP BY UserId, [key]
), p2 AS
(
SELECT MIN(Id) Id, UserId,
JSON_QUERY( '{' + STRING_AGG( '"' + [key] + '"' + ':' + CAST( [value] AS VARCHAR(10) ), ',' ) + '}' ) Platforms
FROM p1
GROUP BY UserId
), t1 AS
(
SELECT MIN(Id) AS Id, UserId, [key], SUM( CAST( [value] AS INT ) ) [value]
FROM jsonWorking
CROSS APPLY OPENJSON ( TransactionTypeId )
GROUP BY UserId, [key]
), t2 AS
(
SELECT MIN(Id) Id, UserId,
JSON_QUERY( '{' + STRING_AGG( '"' + [key] + '"' + ':' + CAST( [value] AS VARCHAR(10) ), ',' ) + '}' ) TransactionTypeId
FROM t1
GROUP BY UserId
)
SELECT p2.Id, p2.UserId, p2.Platforms, t2.TransactionTypeId
FROM p2
INNER JOIN t2 ON p2.UserId = t2.UserId