SQL Server 2016 存储过程中的串联 OPENJSON
A concatenation OPENJSON in a SQL Server 2016 stored procedure
我需要将所有作者合并为一个特定的 UID。基本字段来自另一个 post.
的代码
DECLARE @json NVARCHAR(MAX)
SET @json = '{
"header": {
"type": "esummary",
"version": "0.3"
},
"result": {
"uids": [
"17784783",
"19505939",
"30166592"
],
"17784783": {
"uid": "17784783",
"pubdate": "2007 Aug",
"epubdate": "2007 Jul 20",
"source": "PLoS Comput Biol",
"sortpubdate": "2007/08/01 00:00",
"authors": [
{
"name": "Yu Y",
"authtype": "Author",
"clusterid": ""
},
{
"name": "Wang G",
"authtype": "Author",
"clusterid": ""
},
{
"name": "Simha R",
"authtype": "Author",
"clusterid": ""
}
]
,
"19505939": {
"uid": "19505939",
"pubdate": "2009 Aug 1",
"epubdate": "2009 Jun 8",
"source": "Bioinformatics",
"sortpubdate": "2009/08/01 00:00"
},
"authors": [
{
"name": "Zang C",
"authtype": "Author",
"clusterid": ""
}],
"30166592": {
"uid": "30166592",
"pubdate": "2019 Jan",
"epubdate": "2018 Aug 30",
"source": "Oncogene",
"sortpubdate": "2019/01/01 00:00",
"authors": [
{
"name": "Sun J",
"authtype": "Author",
"clusterid": ""
},
{
"name": "Cai X",
"authtype": "Author",
"clusterid": ""
}],
}
}
}'
最后我想
uid sortpubdate epubdate Authors
-----------------------------------------------------------------------
17784783 2007/08/01 00:00 2007 Jul 20 Yu Y,Wang G,Simha R
19505939 2009/08/01 00:00 2009 Jun 8 Simha R
30166592 2019/01/01 00:00 2018 Aug 30 Sun J, Cai, X
我在 上从 Zohar Peled 那里得到了很好的回答,对第一部分有帮助,现在我需要看看我是否能完成这个。
SELECT [uid], [sortpubdate], [epubdate]
FROM OPENJSON(@json, N'$.result') AS items
CROSS APPLY
-- parse each object in the array
OPENJSON(items.[value])
WITH(
[uid] nvarchar(max) N'$.uid' ,
[sortpubdate] nvarchar(max) N'$.sortpubdate',
[epubdate] nvarchar(max) N'$.epubdate'
) As content
WHERE [key] <> 'uids' -- Get only the relevant content
你发的json有点乱,我编辑了一下。
下一个查询应该使用常见的 table 表达式以及 stuff
和 for xml
.
的组合为您提供包括作者姓名在内的结果
固定json:
DECLARE @json NVARCHAR(MAX) =
'{
"header": {
"type": "esummary",
"version": "0.3"
},
"result": {
"17784783": {
"uid": "17784783",
"pubdate": "2007 Aug",
"epubdate": "2007 Jul 20",
"source": "PLoS Comput Biol",
"sortpubdate": "2007/08/01 00:00",
"authors": [
{
"name": "Yu Y",
"authtype": "Author",
"clusterid": ""
},
{
"name": "Wang G",
"authtype": "Author",
"clusterid": ""
},
{
"name": "Simha R",
"authtype": "Author",
"clusterid": ""
}
]
},
"19505939": {
"uid": "19505939",
"pubdate": "2009 Aug 1",
"epubdate": "2009 Jun 8",
"source": "Bioinformatics",
"sortpubdate": "2009/08/01 00:00",
"authors": [
{
"name": "Zang C",
"authtype": "Author",
"clusterid": ""
}
]
},
"30166592": {
"uid": "30166592",
"pubdate": "2019 Jan",
"epubdate": "2018 Aug 30",
"source": "Oncogene",
"sortpubdate": "2019/01/01 00:00",
"authors": [
{
"name": "Sun J",
"authtype": "Author",
"clusterid": ""
},
{
"name": "Cai X",
"authtype": "Author",
"clusterid": ""
}
]
},
"uids": [
"17784783",
"19505939",
"30166592"
]
}
}';
使用常见的 table 表达式从 json:
中提取值
WITH CTE AS
(
SELECT [uid],
[sortpubdate],
[epubdate],
[name]
FROM OPENJSON(@json, N'$.result') AS items
CROSS APPLY
-- parse each object in the array
OPENJSON(items.[value])
WITH(
[uid] nvarchar(max) N'$.uid' ,
[sortpubdate] nvarchar(max) N'$.sortpubdate',
[epubdate] nvarchar(max) N'$.epubdate',
-- Note the AS JSON on the next row - will not work without it!
[authors] nvarchar(max) N'$.authors' AS JSON
) As content
CROSS APPLY
OPENJSON([authors])
WITH ([name] nvarchar(max) N'$.name')
As authorsNames
WHERE items.[key] <> 'uids' -- Get only the relevant content
)
并使用 stuff
和 for xml
查询 cte:
SELECT DISTINCT [uid],
[sortpubdate],
[epubdate],
STUFF((
SELECT ',' + [name]
FROM CTE As t1
WHERE t1.[uid] = t0.[uid]
FOR XML PATH('')
), 1, 1, '') As authors
FROM CTE As t0
结果:
uid sortpubdate epubdate authors
17784783 2007/08/01 00:00 2007 Jul 20 Yu Y,Wang G,Simha R
19505939 2009/08/01 00:00 2009 Jun 8 Zang C
30166592 2019/01/01 00:00 2018 Aug 30 Sun J,Cai X
我需要将所有作者合并为一个特定的 UID。基本字段来自另一个 post.
的代码DECLARE @json NVARCHAR(MAX)
SET @json = '{
"header": {
"type": "esummary",
"version": "0.3"
},
"result": {
"uids": [
"17784783",
"19505939",
"30166592"
],
"17784783": {
"uid": "17784783",
"pubdate": "2007 Aug",
"epubdate": "2007 Jul 20",
"source": "PLoS Comput Biol",
"sortpubdate": "2007/08/01 00:00",
"authors": [
{
"name": "Yu Y",
"authtype": "Author",
"clusterid": ""
},
{
"name": "Wang G",
"authtype": "Author",
"clusterid": ""
},
{
"name": "Simha R",
"authtype": "Author",
"clusterid": ""
}
]
,
"19505939": {
"uid": "19505939",
"pubdate": "2009 Aug 1",
"epubdate": "2009 Jun 8",
"source": "Bioinformatics",
"sortpubdate": "2009/08/01 00:00"
},
"authors": [
{
"name": "Zang C",
"authtype": "Author",
"clusterid": ""
}],
"30166592": {
"uid": "30166592",
"pubdate": "2019 Jan",
"epubdate": "2018 Aug 30",
"source": "Oncogene",
"sortpubdate": "2019/01/01 00:00",
"authors": [
{
"name": "Sun J",
"authtype": "Author",
"clusterid": ""
},
{
"name": "Cai X",
"authtype": "Author",
"clusterid": ""
}],
}
}
}'
最后我想
uid sortpubdate epubdate Authors
-----------------------------------------------------------------------
17784783 2007/08/01 00:00 2007 Jul 20 Yu Y,Wang G,Simha R
19505939 2009/08/01 00:00 2009 Jun 8 Simha R
30166592 2019/01/01 00:00 2018 Aug 30 Sun J, Cai, X
我在
SELECT [uid], [sortpubdate], [epubdate]
FROM OPENJSON(@json, N'$.result') AS items
CROSS APPLY
-- parse each object in the array
OPENJSON(items.[value])
WITH(
[uid] nvarchar(max) N'$.uid' ,
[sortpubdate] nvarchar(max) N'$.sortpubdate',
[epubdate] nvarchar(max) N'$.epubdate'
) As content
WHERE [key] <> 'uids' -- Get only the relevant content
你发的json有点乱,我编辑了一下。
下一个查询应该使用常见的 table 表达式以及 stuff
和 for xml
.
固定json:
DECLARE @json NVARCHAR(MAX) =
'{
"header": {
"type": "esummary",
"version": "0.3"
},
"result": {
"17784783": {
"uid": "17784783",
"pubdate": "2007 Aug",
"epubdate": "2007 Jul 20",
"source": "PLoS Comput Biol",
"sortpubdate": "2007/08/01 00:00",
"authors": [
{
"name": "Yu Y",
"authtype": "Author",
"clusterid": ""
},
{
"name": "Wang G",
"authtype": "Author",
"clusterid": ""
},
{
"name": "Simha R",
"authtype": "Author",
"clusterid": ""
}
]
},
"19505939": {
"uid": "19505939",
"pubdate": "2009 Aug 1",
"epubdate": "2009 Jun 8",
"source": "Bioinformatics",
"sortpubdate": "2009/08/01 00:00",
"authors": [
{
"name": "Zang C",
"authtype": "Author",
"clusterid": ""
}
]
},
"30166592": {
"uid": "30166592",
"pubdate": "2019 Jan",
"epubdate": "2018 Aug 30",
"source": "Oncogene",
"sortpubdate": "2019/01/01 00:00",
"authors": [
{
"name": "Sun J",
"authtype": "Author",
"clusterid": ""
},
{
"name": "Cai X",
"authtype": "Author",
"clusterid": ""
}
]
},
"uids": [
"17784783",
"19505939",
"30166592"
]
}
}';
使用常见的 table 表达式从 json:
中提取值WITH CTE AS
(
SELECT [uid],
[sortpubdate],
[epubdate],
[name]
FROM OPENJSON(@json, N'$.result') AS items
CROSS APPLY
-- parse each object in the array
OPENJSON(items.[value])
WITH(
[uid] nvarchar(max) N'$.uid' ,
[sortpubdate] nvarchar(max) N'$.sortpubdate',
[epubdate] nvarchar(max) N'$.epubdate',
-- Note the AS JSON on the next row - will not work without it!
[authors] nvarchar(max) N'$.authors' AS JSON
) As content
CROSS APPLY
OPENJSON([authors])
WITH ([name] nvarchar(max) N'$.name')
As authorsNames
WHERE items.[key] <> 'uids' -- Get only the relevant content
)
并使用 stuff
和 for xml
查询 cte:
SELECT DISTINCT [uid],
[sortpubdate],
[epubdate],
STUFF((
SELECT ',' + [name]
FROM CTE As t1
WHERE t1.[uid] = t0.[uid]
FOR XML PATH('')
), 1, 1, '') As authors
FROM CTE As t0
结果:
uid sortpubdate epubdate authors
17784783 2007/08/01 00:00 2007 Jul 20 Yu Y,Wang G,Simha R
19505939 2009/08/01 00:00 2009 Jun 8 Zang C
30166592 2019/01/01 00:00 2018 Aug 30 Sun J,Cai X