0

我正在尝试以特定的 JSON 格式创建一些输出,这是一个输出示例(将重复多次 - 每所大学一次):

示例 json 图像

{
    "id": "37e556ae6b9f620d2b7262d3de971c40",
    "recordType": "school",
    "created": 1532531378,
    "updated": 1532531378,
    "published": 1532531378,
    "name": "ABC University – ABC School of Management",
    "schoolName": "ABC School of Management",
    "universityName": "ABC University",
    "sortableName": "ABC School of Management"
}

我一直在想我会为此使用 SQL,因为它具有处理 JSON 格式并将处理嵌套的 FOR JSON 命令。当我对值进行硬编码时,我可以使用以下代码重新创建上述内容:

SELECT 
     HASHBYTES('md5','something') AS id
     ,'school' AS recordType
     ,getdate() AS created
     ,getdate() AS updated
     ,getdate() AS published
     ,'ABC University – ABC School of Management' AS name
     ,'ABC School of Management' as schoolname 
     ,'ABC University' as universityName
     , 'ABC School of Management' as sortableName
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER 

我遇到的问题是我需要为不同的大学创建此输出 100 次。我想在 SQL 中创建一个数据库表并在其上运行一些东西。这是 Excel 中的数据示例,我已过滤列表以仅显示上述所需的数据以及稍后单独输出中所需的其他几个数据点(我将担心其他输出之后):

IndiUID SchoolCode  DataValue   Ranking Band    RegionalRank
OVERALL_SCORE   XYZ     105 D   23
SCHOOL_NAME_FORMAL  XYZ XYZ Institute of Management         
SCHOOL_NAME_SORTABLE    XYZ XYZ Institute of Management         
SCHOOL_NAME_AND_BUSINESS_SCHOOL_NAME    XYZ XYZ Institute of Management, XYZ School of Business         
STUDENTS_MEDIAN_AGE XYZ 26          
OVERALL_SCORE   ABC     35  B   3
SCHOOL_NAME_FORMAL  ABC ABC School of Management            
SCHOOL_NAME_SORTABLE    ABC ABC School of Management            
SCHOOL_NAME_AND_BUSINESS_SCHOOL_NAME    ABC ABC University – ABC School of Management           
STUDENTS_MEDIAN_AGE ABC 26          

数据样本图像

我只是无法完全理解我需要如何构建我的表,以便我可以使用 SQL 提取相关部分。我想知道是否应该旋转表,以便“IndiUID”的值成为数据库表中的字段,或者为每个当前标题项(DataValue、Ranking、Band 等)创建一个新表。

我想我可以从 indiUID = OVERALL_SCORE、SCHOOL_NAME_FORMAL 等的表中进行选择,但我想知道这是否太混乱了。

这就是我卡住的地方——我如何将这些数据组织到表格中以便能够提取它?这主要是关于创建 JSON 输出,因此 DB 部分是否是一个特别优雅的解决方案并不重要(它不会是 - 我对 SQL 非常基础!)。

4

1 回答 1

0

你可以试试以下

样本数据

CREATE TABLE [#school]
([school_name_and_business_school_name] VARCHAR(255), 
 [school_name]                          VARCHAR(255), 
 [university_name]                      VARCHAR(255), 
 [school_sortable]                      VARCHAR(255)
);

INSERT INTO [#school]
VALUES
('ABC University – ABC School of Management', 
 'ABC School of Management', 
 'ABC University', 
 'ABC School of Management'
),
('XYZ University – XYZ School of Management', 
 'XYZ School of Management', 
 'XYZ University', 
 'XYZ School of Management'
),
('QWE University – QWEC School of Management', 
 'QWE School of Management', 
 'QWE University', 
 'QWE School of Management'
);

然后使用这个查询:

SELECT HASHBYTES('md5', 'something') AS [id], 
  'school' AS [recordType], 
  GETDATE() AS [created], 
  GETDATE() AS [updated], 
  GETDATE() AS [published], 
  [school_name_and_business_school_name] AS [name], 
  [school_name] AS [schoolName], 
  [university_name] AS [universityName], 
  [school_sortable] AS [sortableName]
FROM   [#school] 
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER;

为您提供以下结果:

{"id":"Q3uTDbhLgHnC3YBKcZNrXw==",
"recordType":"school",
"created":"2018-09-07T10:14:44.130",
"updated":"2018-09-07T10:14:44.130",
"published":"2018-09-07T10:14:44.130",
"name":"ABC University – ABC School of Management",
"schoolName":"ABC School of Management",
"universityName":"ABC University",
"sortableName":"ABC School of Management"
},

{"id":"Q3uTDbhLgHnC3YBKcZNrXw==",
"recordType":"school",
"created":"2018-09-07T10:14:44.130",
"updated":"2018-09-07T10:14:44.130",
"published":"2018-09-07T10:14:44.130",
"name":"XYZ University – XYZ School of Management",
"schoolName":"XYZ School of Management",
"universityName":"XYZ University",
"sortableName":"XYZ School of Management"
},

{"id":"Q3uTDbhLgHnC3YBKcZNrXw==",
"recordType":"school",
"created":"2018-09-07T10:14:44.130",
"updated":"2018-09-07T10:14:44.130",
"published":"2018-09-07T10:14:44.130",
"name":"QWE University – QWEC School of Management",
"schoolName":"QWE School of Management",
"universityName":"QWE University",
"sortableName":"QWE School of Management"
}

这有帮助吗?

于 2018-09-07T08:23:41.683 回答