在SQL Server 2016中嵌套JSON

时间:2018-02-09 05:21:43

标签: json sql-server

我有下表:

CREATE TABLE #Temp
    (
        customerId INT
      , storeCity VARCHAR(50)
      , transactionDescription VARCHAR(200)
      , monthYear VARCHAR(20)
      , monthYearOrder INT
      , amount DECIMAL(18, 2)
    )


INSERT INTO #Temp ( customerId
                  , storeCity
                  , transactionDescription
                  , monthYear
                  , monthYearOrder
                  , amount )
VALUES ( 1, 'Nashville', 'Gas', 'Jan 2017', 1, 67 )
     , ( 1, 'Nashville', 'Gas', 'Feb 2017', 2, 98 )
     , ( 1, 'Nashville', 'Gas', 'Mar 2017', 3, 46 )
     , ( 1, 'Nashville', 'Gas', 'Apr 2017', 4, 56 )
     , ( 1, 'Nashville', 'Gas', 'May 2017', 5, 67 )
     , ( 1, 'Nashville', 'Gas', 'Jun 2017', 6, 76 )
     , ( 1, 'Nashville', 'Food', 'Jan 2017', 1, 10 )
     , ( 1, 'Nashville', 'Food', 'Feb 2017', 2, 11 )
     , ( 1, 'Nashville', 'Food', 'Mar 2017', 3, 12 )
     , ( 1, 'Nashville', 'Food', 'Apr 2017', 4, 13 )
     , ( 1, 'Nashville', 'Food', 'May 2017', 5, 14 )
     , ( 1, 'Nashville', 'Food', 'Jun 2017', 6, 15 )
     , ( 2, 'Nashville', 'Gas', 'Jan 2017', 1, 100 )
     , ( 2, 'Nashville', 'Gas', 'Feb 2017', 2, 198 )
     , ( 2, 'Nashville', 'Gas', 'Mar 2017', 3, 146 )
     , ( 2, 'Nashville', 'Gas', 'Apr 2017', 4, 65 )
     , ( 2, 'Nashville', 'Gas', 'May 2017', 5, 76 )
     , ( 2, 'Nashville', 'Gas', 'Jun 2017', 6, 23 )
     , ( 2, 'Nashville', 'Food', 'Jan 2017', 1, 20 )
     , ( 2, 'Nashville', 'Food', 'Feb 2017', 2, 19 )
     , ( 2, 'Nashville', 'Food', 'Mar 2017', 3, 18 )
     , ( 2, 'Nashville', 'Smokes', 'Jan 2017', 1, 8 )
     , ( 2, 'Nashville', 'Smokes', 'Feb 2017', 2, 8 )
     , ( 2, 'Nashville', 'Smokes', 'Mar 2017', 3, 8 )

我需要根据customerId转换为JSON,但我想要嵌套所有MonthYear和Amounts。我customerId 1的最终结果是:

    [
  {
    "storeCity": "Nashville",
    "transactionDescription": "Gas",
    "months": {
      "Jan 2017": 67,
      "Feb 2017": 98,
      "Mar 2017": 46,
      "Apr 2017": 56,
      "May 2017": 67,
      "Jun 2017": 76
    }
  },
  {
    "storeCity": "Nashville",
    "transactionDescription": "Food",
    "months": {
      "Jan 2017": 10,
      "Feb 2017": 11,
      "Mar 2017": 12,
      "Apr 2017": 13,
      "May 2017": 14,
      "Jun 2017": 15
    }
  }
]

首先,我有:

SELECT DISTINCT t.customerId
              , (   SELECT storeCity
                         , transactionDescription
                         , monthYear
                         , amount
                    FROM   #Temp
                    WHERE  t.customerId = #Temp.customerId
                    FOR JSON AUTO )
FROM   #Temp t

但我不确定如何嵌套monthYearamount值。我首先尝试的一件事是转动#temp表,因此所有monthYear值都是列,但由于我的要求的性质,我无法进行转轴,因为MonthYear列中的值会有所不同,在这种情况下,使用动态查询来旋转数据并不合适。 可以这样做吗? 感谢

1 个答案:

答案 0 :(得分:0)

请试试这个

                 SELECT DISTINCT customerId,       
                   (SELECT distinct storeCity, transactionDescription ,
                     (SELECT concat(monthYear , ':', amount )   m
                      FROM #Temp AS months 
                      WHERE #Temp.customerId= months.customerId 
                      FOR JSON  auto   
                    ) as months
                   FROM #Temp 
                   WHERE  t.customerId = #Temp.customerId
                   FOR JSON auto) 
                 FROM #temp t