SQL Server 2016 - JSON格式查询结果

时间:2017-06-20 19:34:23

标签: sql-server json sql-server-2016

我正在使用SQL server 2016有这个查询:

SELECT TOP (100) 
        brm.practice,
        (select count(*) from _rl_metadata where practice=brm.practice) As TotalPractice,
        brm.primary_subject_area,
        (select count(*) from _rl_metadata where primary_subject_area=brm.primary_subject_area) As TotalSubject,
        brm.content_id,
        brm.content_title
  FROM [_bersin_rl_metadata] AS brm 
    Where brm.is_archive <> 1 and brm.is_published_to_site = 1 

在这里输入代码

来自此表:

CREATE TABLE [dbo].[_rl_metadata](
    [content_id] [bigint] NOT NULL,
    [content_title] [varchar](200) NULL,
    [publish_date] [datetime] NULL,
    [practice] [nvarchar](50) NULL,
    [primary_subject_area] [nvarchar](50) NULL
)

返回这些结果:

enter image description here

我希望以分层JSON格式显示这些结果(我想在像这样的径向d3图表中使用它:https://bl.ocks.org/mbostock/4348373)按实践中的资产数量分组,然后按主题,并显示属性每个资产(例如标题,ID,发布日期)都是这样的:

{
    "name": "Research",
    "children": [{
        "name": "Human Resources",
        "size": 290,
        "children": [{
                "name": "HR & Talent Analytics",
                "size": 75,
                "children": [{ "name": "People Analytics Framework" }, { "name": "Data, Big Data and You" }, ...]
            },
            {
                "name": "HR Org. & Governance",
                "size": 52,
                "children": [{ "name": "Structuring the HR Business" }, { "name": "Relationship Management" }, ...]
            },... 
        ]
    }]
}

使用SQL Server 2016获取此结构的最佳方法是什么?

1 个答案:

答案 0 :(得分:2)

尝试以下解决方案:

DECLARE @SourceTable TABLE (
    Level1_Name NVARCHAR(50) NOT NULL,
    Level1_Size INT NOT NULL,
    Level2_Name NVARCHAR(50) NOT NULL,
    Level2_Size INT NOT NULL,
    Content     NVARCHAR(100) NOT NULL
);
INSERT  @SourceTable
VALUES  
('Leadership', 270, 'Solutions', 70,    'Book #1'),
('Leadership', 270, 'Solutions', 70,    'Book #2'),
('Leadership', 270, 'Strategy', 121,    'Book #3'), 
('Leadership', 270, 'Strategy', 121,    'Book #4'), 
('Leadership', 270, 'Strategy', 121,    'Book #5'), 
('Leadership', 270, 'Development', 10,  'Book #6'), 
('Coco Jambo', 111, 'Solutions', 111, 'Book #111');

SELECT 
    name    = 'Root object',
    children= (
        SELECT  DISTINCT 
            name    = lvl1.Level1_Name, 
            size    = lvl1.Level1_Size,
            children= (
                SELECT  DISTINCT 
                    name    = lvl2.Level2_Name, 
                    size    = lvl2.Level2_Size,
                    children= (
                        SELECT  DISTINCT 
                            name    = lvl3.Content 
                        FROM    @SourceTable lvl3
                        WHERE   lvl2.Level1_Name = lvl1.Level1_Name 
                        AND     lvl3.Level2_Name = lvl2.Level2_Name
                        FOR JSON PATH
                    )
                FROM    @SourceTable lvl2
                WHERE   lvl2.Level1_Name = lvl1.Level1_Name
                FOR JSON PATH
            )
        FROM    @SourceTable lvl1
        FOR JSON PATH
    )
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER

结果:

{
  "name": "Root object",
  "children": [
    {
      "name": "Leadership",
      "size": 270,
      "children": [
        {
          "name": "Development",
          "size": 10,
          "children": [
            {
              "name": "Book #6"
            }
          ]
        },
        {
          "name": "Solutions",
          "size": 70,
          "children": [
            {
              "name": "Book #1"
            },
            {
              "name": "Book #111"
            },
            {
              "name": "Book #2"
            }
          ]
        },
 ...

<强> Demo