Eu quero usar a JSON_QUERY
com uma SELECT TOP 1
consulta de modo que o json resultante tenha esse registro top 1 em forma de objeto, em vez de em forma de tabela?
Por exemplo, a seguinte consulta ( demonstração ao vivo ):
CREATE TABLE Trees
(
[Id] INT,
[Type] NVARCHAR(100),
[Height] DECIMAL(2,1)
);
INSERT INTO Trees ([Id], [Type], [Height])
VALUES
(1, 'Palm', 5.5),
(2, 'Pine', 6.2),
(3, 'Apple', 2.5),
(4, 'Japanese Cedar', 0.5),
(5, 'Spanish Fir', 0.6);
SELECT
highestTree = JSON_QUERY(
(
SELECT TOP 1
Id as id,
Type as type,
Height as height
FROM Trees
WHERE Height = (SELECT Max(Height) FROM Trees)
FOR JSON PATH
)
),
lowestTree = JSON_QUERY(
(
SELECT TOP 1
Id as id,
Type as type,
Height as height
FROM Trees
WHERE Height = (SELECT MIN(Height) FROM Trees)
FOR JSON PATH
)
)
FOR JSON
PATH, WITHOUT_ARRAY_WRAPPER
;
saídas:
{"highestTree":[{"id":2,"type":"Pine","height":6.2}],"lowestTree":[{"id":4,"type":"Japanese Cedar","height":0.5}]}
Mas eu quero:
{"highestTree":{"id":2,"type":"Pine","height":6.2},"lowestTree":{"id":4,"type":"Japanese Cedar","height":0.5}}
Tente este :
Resultado: