【问题标题】:SQL OPENJSON get only first elementSQL OPENJSON 只获取第一个元素
【发布时间】: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"? - 请添加一个标签以指定您使用的是mysqlpostgresqlsql-serveroracle 还是db2 - 或其他完全不同的东西。

标签: sql arrays json


【解决方案1】:

你有一个数组中的数组,所以你必须写。

JSOn 是一个有趣的数据结构,但你需要大量的经验才能使用它

 SELECT Effective_Moment, Entry_Moment  
 FROM [Daily_And_Future_Terminations_Transaction_Log_Source] 
 CROSS APPLY OPENJSON (Transaction_Log_Entry_Data, '$[0]."wd:Transaction_Log_Entry"[0]')
 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"'
       )

Effective_Moment Entry_Moment
2023-01-18T09:00:00+01:00 2023-01-19T10:49:00.868+01:00

fiddle

【讨论】:

  • 我现在才看到 :) 它当然是 arry 中的第一个元素。我的注意力在别处。我的备份只是使用 Select top(1) ,它确实有效,但另一种方式更优雅。我还研究了获取 Entry Moment 的 MAX 值,因此不依赖于元素的顺序 - Thx nbk
猜你喜欢
  • 1970-01-01
  • 2019-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-17
  • 2020-04-07
  • 1970-01-01
  • 2020-03-07
相关资源
最近更新 更多