我需要为特定的 UID 组合所有作者。基本领域是在另一篇文章的代码中工作的。
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
我在 SQL Server 2016 中的存储过程中的 OPENJSON 语法中得到了 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