I have a table with following fields
Id RequestId CategoryId
1 112 1
2 123 1
3 123 2
SELECT R.RequestId,
(SELECT RC.CategoryId FROM Request RC WHERE RC.Id = R.Id FOR JSON AUTO) AS Categories
FROM Request R
Above query returns the data as mentioned below
RequestId Categories
112 [{"CategoryId":"1"}]
123 [{"CategoryId":"1"},{"CategoryId":"2"}]
But, I want that column name CategoryId should not be repeated for every item in json array. Thus, my expected result is:
RequestId Categories
112 ["1"]
123 ["1","2"]