SQL Server : query columns to JSON object with group by

后端 未结 4 2060
有刺的猬
有刺的猬 2021-02-14 08:16

I have a table with 3 columns, I want to query that table such that the result will be a JSON object.

Sample data looks like this:

 CREATE TABLE #Test (         


        
4条回答
  •  忘掉有多难
    2021-02-14 09:12

    Posting this if any one has the same sort of use case

    SELECT [t].[ID], 
    (SELECT [t1].[KEYS], [t1].[ValueV] FROM @Test t1 WHERE t1.[ID] = [t].id FOR JSON PATH ) a
    FROM @Test AS [t]
    GROUP BY [t].[ID]
    

提交回复
热议问题