【发布时间】:2023-02-05 01:00:23
【问题描述】:
我试图仅从 JSON 字符串中的第一个元素获取数据,因为第一个元素始终是最新的。我尝试了一些没有运气的事情。也许这里有人可以指出我正确的方向。
这是 JSON 字符串:
[
{
"wd:Transaction_Log_Entry": [
{
"wd:Transaction_Log_Reference": {
"@wd:Descriptor": "Rescind of End Contract: XXXX (Rescinded)",
"wd:ID": {
"@wd:type": "WID",
"#text": "fb27bafef89b101b5bf865947b420000"
}
},
"wd:Transaction_Log_Data": {
"wd:Transaction_Log_Description": "Rescind of End Contract: XXXX (Rescinded)",
"wd:Transaction_Effective_Moment": "2023-01-18T09:00:00+01:00",
"wd:Transaction_Entry_Moment": "2023-01-19T10:49:00.868+01:00",
"wd:Is_Rescind_Or_Rescinded": "1",
"wd:Is_Correction_Or_Corrected": "0"
}
},
{
"wd:Transaction_Log_Reference": {
"@wd:Descriptor": "End Contract: XXXX (Rescinded)",
"wd:ID": {
"@wd:type": "WID",
"#text": "a4a0fd2c2df8101bd1c6bde5f5710000"
}
},
"wd:Transaction_Log_Data": {
"wd:Transaction_Log_Description": "End Contract: XXXX (Rescinded)",
"wd:Transaction_Effective_Moment": "2023-01-18T09:00:00+01:00",
"wd:Transaction_Entry_Moment": "2023-01-18T12:41:43.867+01:00",
"wd:Is_Rescind_Or_Rescinded": "1",
"wd:Is_Correction_Or_Corrected": "0"
}
}
]
}
]
我的 SQL 查询 - 我从列名为“Transaction_Log_Entry_Data”的表中选择 JSON 字符串
SELECT Effective_Moment, Entry_Moment
FROM [dbo].[Daily_And_Future_Terminations_Transaction_Log_Source]
CROSS APPLY OPENJSON (Transaction_Log_Entry_Data, '$[0]."wd:Transaction_Log_Entry"')
WITH (
Effective_Moment NVARCHAR(50) '$."wd:Transaction_Log_Data"."wd:Transaction_Effective_Moment"',
Entry_Moment NVARCHAR(50) '$."wd:Transaction_Log_Data"."wd:Transaction_Entry_Moment"'
)
我的结果是 2 行,我只想要第一个元素的数据:
此致 奥莱
【问题讨论】:
-
Why should I "tag my RDBMS"? - 请添加一个标签以指定您使用的是
mysql、postgresql、sql-server、oracle还是db2- 或其他完全不同的东西。