2017-06-20 50 views
2

我使用SQL Server 2016有這個疑問:的SQL Server 2016 - JSON格式的查詢結果

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 
) 

返回這些:

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 

從這個表中輸入代碼在這裏

結果:

enter image description here

我想以分層JSON格式顯示這些結果(我想在徑向d3圖表中使用它,如下所示:https://bl.ocks.org/mbostock/4348373),按實踐中的資產數量,然後顯示Subject,並顯示每個資產的屬性(例如,標題,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獲得此結構的最佳方式?

+0

周圍有一些實例中,如G。 https://docs.microsoft.com/en-us/sql/relational-databases/json/format-query-results-as-json-with-for-json-sql-server – IngoB

回答

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

+0

注意:'Level1 {1 | 2 } _Name'列應該是強制的/ NOT NULL。 –

+0

謝謝!這看起來非常可擴展。一個問題,如果我想在內容列中有幾個字段(例如id,asset_name,pub_date),那麼你會將這些全部內容填入內容列並分隔,或者將每個字段添加到源表(而不是內容有id,asset_name,pub_date)? –

+0

@ lance-p:>我會將這些信息保存爲表格中的單獨列。 –